Skip to content

Instantly share code, notes, and snippets.

@hatched
Created February 24, 2011 17:26
Show Gist options
  • Save hatched/842497 to your computer and use it in GitHub Desktop.
Save hatched/842497 to your computer and use it in GitHub Desktop.
/**
* Adds a method to the Y.Base prototype to handle events so that when a widget
* or plugin becomes destroyed all events attached are detached.
*
* Requires Y.Lang & Y.Array
*/
YUI.add('event-handler', function(Y) {
var YArray = Y.Array;
Y.Base.prototype.eventHandler = {
add : function(event) {
this.events = this.events || [];
if (YLang.isArray(event)) {
YArray.each(event, function(ev) {
this.events.push(ev);
}, this);
} else {
this.events.push(event);
}
},
destroy : function() {
YArray.each(this.events, function(eh) {
eh.detach();
});
}
}
});
-= To Use Within Widget Or Plugin=-
-=To Add=-
this.eventHandler.add([
Y.one('window').on('resize', function, this),
Y.one('#loginform').on('submit', function, this)
]);
-=OR=-
this.eventHandler.add(
Y.one('#loginform').on('submit', function, this)
);
-=To Destroy=-
this.eventHandler.destroy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment