You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Binding an event handler using one() cannot be removed using off().
Scenario:
var callback = new function() { alert('Do not trigger me!') };
var component = new Component(); // Any CoreObject has the one/off functions
component.one('myEvent', callback);
component.off('myEvent', callback);
component.trigger('myEvent'); // Should not raise alert, but will anyways.
Cause
The vjs.one() function wraps the function passed with an anonymous function:
When vjs.on() is called, it adds a guid to the function, so it can identify and remove it later. When vjs.off() is called, it seeks the function with that guid and removes it. Because callback in my example gets wrapped with an anonymous function, it does not assign the guid to callback, rather to the anonymous function. When vjs.off(callback) is called, it doesn't find a matching guid and thus fails to unbind the event handler.
Proposed Solution
"Sync" the guid on the anonymous function and fn, so they both have the same guid. This will allow event handlers set with vjs.one() to be 'cancelled.'
Sample code:
vjs.one=function(elem,type,fn){varfunc=function(){vjs.off(elem,type,arguments.callee);fn.apply(this,arguments);};// If on() is called before one() is// Inherit the ID of fn, so that one() may be cancelled.if(fn.guid){func.guid=fn.guid;}vjs.on(elem,type,func);// This function will set func.guid if it wasn't previously set.// If one() is called before on()// Set the ID of fn, so that on() may be cancelled.if(!fn.guid){fn.guid=func.guid;}};
This code works on my fork.
The text was updated successfully, but these errors were encountered:
Binding an event handler using one() cannot be removed using off().
Scenario:
Cause
The vjs.one() function wraps the function passed with an anonymous function:
When
vjs.on()
is called, it adds aguid
to the function, so it can identify and remove it later. Whenvjs.off()
is called, it seeks the function with thatguid
and removes it. Becausecallback
in my example gets wrapped with an anonymous function, it does not assign theguid
tocallback
, rather to the anonymous function. Whenvjs.off(callback)
is called, it doesn't find a matchingguid
and thus fails to unbind the event handler.Proposed Solution
"Sync" the
guid
on the anonymous function andfn
, so they both have the sameguid
. This will allow event handlers set withvjs.one()
to be 'cancelled.'Sample code:
This code works on my fork.
The text was updated successfully, but these errors were encountered: