PDA

View Full Version : Observable.fire - behaviour changed.



Animal
23 Feb 2007, 6:21 AM
In the old code, Observable.fireEvent used fireDirect which looked like this:



YAHOO.util.CustomEvent.prototype.fireDirect = function(){
var len=this.subscribers.length;
for (var i=0; i<len; ++i) {
var s = this.subscribers[i];
if(s){
var scope = (s.override) ? s.obj : this.scope;
if(s.fn.apply(scope, arguments) === false){
return false;
}
}
}
return true;
};


See that if the handler does not explicitly return false the event firing code returns true, and if you write your Observable subclass so that handlers may veto the operation by explicitly returning false - your code will still execute if no return statement is executed.

The new code is:



fire : function(){
var args = Array.prototype.slice.call(arguments, 0);
var ls = this.listeners, scope;
for(var i = 0, len = ls.length; i < len; i++){
var l = ls[i];
if(l.fireFn.apply(l.scope, arguments) === false){
return false;
}
}
}


It just needs "return true;" adding to the end!

jack.slocum
23 Feb 2007, 7:19 AM
I added it. Personally, I always check === or !== but I am paranoid. :)