Brk Brk - 3 months ago 18
AngularJS Question

How to pass event and other arguments to click handler

Hey I have a build a canvas using easelJS.
In my canvas, I have points which a click handler is define for them using the following syntax:

p.on("click", handleMouseClickEvent);


Now I want to pass arguments to the handler handleMouseClickEvent , I know that I get the event object for free without passing it, but when I try to pass one argument, lets say I write:

p.on("click", handleMouseClickEvent(arg1));


Then the event object is undefined and not accessible at all.
How can I pass the event object and many more arguments using the above syntax.

p.on("click", handleMouseClickEvent(arg1,arg2,...,argN));

Answer

When using jQuery, Ravi's answer is perhaps the best way. I'll try to provide another perspective to solve your question.

By using

p.on("click", handleMouseClickEvent(arg1));

you're not passing the function as event handler, you're executing it and passing its return value as event handler. That perhaps already pointed you to the answer, right? Try to define your event handler like this:

function handleMouseClickEvent(arg1)) {
    return function reallyHandleMouseClickEvent(event) {
        // all variables available here: arg1, event
    }
}

Of course, you can add as many argN parameters as you want.