public
Last active — forked from ibolmo/Listener.js

  • Download Gist
Listener.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
// Element Listener Mixin
(function(){
 
var setup = function(element){
var listener = new Events, removeEvent = listener.removeEvent;
listener.removeEvent = function(key, value){
removeEvent(key, value);
element.removeEvent(key, value);
};
return listener;
};
 
this.Listener = new Class({
 
attach: function(key, value){
if (!this.$listener) this.$listener = setup(this.toElement());
this.$listener.addEvent(key, value);
this.toElement().addEvent(key, value);
}.overloadSetter(),
 
detach: function(key, value){
if (this.$listener){
if (typeof key == 'string') this.$listener.removeEvent(key, value);
else this.$listener.removeEvents(key);
}
return this;
},
 
toElement: function(){
return this.element;
}
 
});
 
}).call(this);
 
// Implementation
var MyClass = new Class({
Implements: [Listener, Class.Binds],
initialize: function(){
this.element = new Element('div');
this.attach({
click: this.bound('onClick'),
'keyup:relay(:input)': this.bound('onKeyUp')
});
}
});
 
// Use
var myInstance = new MyClass();
myInstance.detach();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.