#8596 closed bug (invalid)
Event $(this).bind conflict with $(this).trigger
Reported by: | Owned by: | ||
---|---|---|---|
Priority: | undecided | Milestone: | 1.next |
Component: | event | Version: | 1.5.1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
There seems to be a conflict when trying to trigger an event that has the same name as a function in a custom javascript object. Example:
var Class = {
construct: function() {
$("body").bind("click", $.proxy(this, "onBodyClick"));
}, onBodyClick: function(e) {
$(this).trigger("onBodyClick");
}
}
The above code will create a loop and the "onBodyClick" function will run over and over again. Not sure what this has to do with, but I've been creating a pretty decent sized application powered by jquery and this error has caught me a few times.
Change History (3)
comment:1 Changed 12 years ago by
comment:2 Changed 12 years ago by
Component: | unfiled → event |
---|---|
Resolution: | → invalid |
Status: | new → closed |
The problem is not jQuery, but that you have misunderstood how you're passing context with the $.proxy call. When I look at your code and I see the result in the console, that's exactly what I expect to see.
comment:3 Changed 12 years ago by
Really? I can accept that. Could you explain why it's happening that way? I thought I was triggering an event, but it seems like it's actually calling a function with that name (and triggering the event). If it's a scope issue with the proxy can you help me understand what is going on there?
Thanks.
Here is the link to the jsfiddle example. http://jsfiddle.net/earl3s/zLeYa/