javascript - Once event fired even if I `$.unbind('event')` all handlers fire. Is there a way to prevent event handlers firing, after event occured? -
i prototyping mechanism allows additional modules subscribe main events of our framework , using array of deferred passed event (convention) control flow of logic.
if example below there 4 event handlers subscribe event, if 1 of deferred being rejected whole mechanism should terminate execution.
prevent event handlers (that modules plugged in) executing @ top of each of handlers checking if
!$.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; if (hasrejected) return false;
once event fired if $.unbind('onprerender')
4 handlers still fire. there way prevent happening, after onprerender occured?
var globaldeferred = $.deferred(function(){settimeout(function(){ console.log('%cglobal deferred','color : blue');}, 0)}); var deferredarray = [globaldeferred.promise()]; dataroles.on('onprerender', function (e, options) { var hasrejected = !$.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; if (hasrejected) return false; var d = $.deferred(); options.deferredarray.push(d.promise()); var rand = math.floor(math.random()*1000); settimeout(function(){ console.log("(1) in "+rand+" ms"); d.resolve(); },rand); return $.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; }); dataroles.on('onprerender', function (e, options) { var hasrejected = !$.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; if (hasrejected) return false; var d = $.deferred(); options.deferredarray.push(d.promise()); var rand = math.floor(math.random()*1000); settimeout(function(){ console.log("(2) in "+rand+" ms"); d.reject(); },rand); }); dataroles.on('onprerender', function (e, options) { var hasrejected = !$.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; if (hasrejected) return false; var d = $.deferred(); options.deferredarray.push(d.promise()); var rand = math.floor(math.random()*1000); settimeout(function(){ console.log("(3) in "+rand+" ms"); d.resolve(); },rand); }); dataroles.on('onprerender', function (e, options) { var hasrejected = !$.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; if (hasrejected) return false; var d = $.deferred(); options.deferredarray.push(d.promise()); var rand = math.floor(math.random()*1000); settimeout(function(){ console.log("(4) in "+rand+" ms"); d.resolve(); },rand); return $.grep(options.deferredarray,function(item){return item.state() == "rejected" }) == 0; }); globaldeferred.reject(); $.when.apply($, deferredarray).then(function () { console.log('%csuccess','color : green'); },function () { console.log("%cfail","color : red"); }); dataroles.trigger('onprerender', { deferredarray : deferredarray});
this fix problem. can replace unbind/off
e.stopimmediatepropagation();
jsfiddle: http://jsfiddle.net/grimbode/qxm2y/2/
Comments
Post a Comment