I’m using the new on() / off() methods from jQuery 1.7, and reading off() documentation I see the following sentence:
Handlers proxied by jQuery.proxy() or a similar mechanism will all have the same unique id (the proxy function), so passing proxied handlers to .off may remove more handlers than intended. In those situations it is better to attach and remove event handlers using namespaces.
That’s the case I’m facing:
function show(){
this.$element.on( 'click', '.close', $.proxy(this.hide, this) );
}
function hide(){
this.$element.off( 'click', '.close', $.proxy(this.hide, this) );
}
If I understand correctly, all calls to $.proxy() has the same uid, so I may deattach more handlers than required…
If that’s correct, I don’t understand (don’t know) how I can attach the handlers using namespaces.
Event namespaces are essential ways to bundle in a group of related events – for example you may wish to use namespacing when creating a plugin, ie:
So later on, maybe on destroy/teardown you can do the following:
And all of the binds you defined will be removed (saves you from removing each individually).
In your case this will allow you to remove events for a particular namespace without removing events for other binds defined by the
$.proxy()callback function outside of your namespace.