IEvents demo
JavaScript
;(function() {
var removeOn = function(string) {
return string.replace(/^on([A-Z])/, function(full, first) {
return first.toLowerCase();
});
};
var IEvents = this.IEvents = new Class({
Extends: Events,
fireEvent: function fireEvent(type, args, delay) {
type = removeOn(type);
if (this.cancellables && this.cancellables.contains(type)) {
for (var key in this.$events[type]) if (this.$events[type].hasOwnProperty(key)) {
if (!this.$events[type][key].apply(this, Array.from(args))) return false;
}
return this;
} else return this.parent(type, args, delay || 1);
}
});
}).apply(this, []);
var myClass = new Class({
Implements: [IEvents],
cancellables: ['foo'],
foo: function() {
if (this.fireEvent('foo')) console.log('foo::finish');
else console.log('foo::faild');
},
bar: function() {
this.fireEvent('bar');
console.log('bar finished');
}
});
var a = new myClass();
a.addEvent('foo', function() {
console.log('foo-should fire');
return false;
});
a.addEvent('foo', function() {
console.log('foo-should not fire');
});
a.foo();
a.addEvent('bar',function(){
console.log('bar still works');
});
a.addEvent('bar',function(){
console.log(abc);//throws an error
});
a.bar();