Created
May 27, 2009 14:36
-
-
Save howardr/118668 to your computer and use it in GitHub Desktop.
EventManager v1.0.1, an easy custom event manager for javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* EventManager, v1.0.1 | |
* | |
* Copyright (c) 2009, Howard Rauscher | |
* Licensed under the MIT License | |
*/ | |
(function() { | |
function EventManager() { | |
this._listeners = {}; | |
} | |
EventManager.prototype = { | |
addListener : function(name, fn) { | |
(this._listeners[name] = this._listeners[name] || []).push(fn); | |
return this; | |
}, | |
removeListener : function(name, fn) { | |
if(arguments.length === 1) { // remove all | |
this._listeners[name] = []; | |
} | |
else if(typeof(fn) === 'function') { | |
var listeners = this._listeners[name]; | |
if(listeners !== undefined) { | |
var foundAt = -1; | |
for(var i = 0, len = listeners.length; i < len && foundAt === -1; i++) { | |
if(listeners[i] === fn) { | |
foundAt = i; | |
} | |
} | |
if(foundAt >= 0) { | |
listeners.splice(foundAt, 1); | |
} | |
} | |
} | |
return this; | |
}, | |
fire : function(name, args) { | |
var listeners = this._listeners[name]; | |
args = args || []; | |
if(listeners !== undefined) { | |
var data = {}, evt; | |
for(var i = 0, len = listeners.length; i < len; i++) { | |
evt = new EventManager.EventArg(name, data); | |
listeners[i].apply(window, args.concat(evt)); | |
data = evt.data; | |
if(evt.removed) { | |
listeners.splice(i, 1); | |
len = listeners.length; | |
--i; | |
} | |
if(evt.cancelled) { | |
break; | |
} | |
} | |
} | |
return this; | |
}, | |
hasListeners : function(name) { | |
return (this._listeners[name] === undefined ? 0 : this._listeners[name].length) > 0; | |
} | |
}; | |
EventManager.eventify = function(object, manager) { | |
var methods = EventManager.eventify.methods; | |
manager = manager || new EventManager(); | |
for(var i = 0, len = methods.length; i < len; i++) (function(method) { | |
object[method] = function() { | |
return manager[method].apply(manager, arguments); | |
}; | |
})(methods[i]); | |
return manager; | |
}; | |
EventManager.eventify.methods = ['addListener', 'removeListener', 'fire']; | |
EventManager.EventArg = function(name, data) { | |
this.name = name; | |
this.data = data; | |
this.cancelled = false; | |
this.removed = false; | |
}; | |
EventManager.EventArg.prototype = { | |
cancel : function() { | |
this.cancelled = true; | |
}, | |
remove : function() { | |
this.removed = true; | |
} | |
}; | |
window.EventManager = EventManager; | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* EventManager Examples | |
*/ | |
// remember to include EventManger source | |
/* | |
* Create a simple event manager | |
*/ | |
// basic | |
var manager = new EventManager(); | |
manager.addListener('myEventName', function() { | |
alert('event thrown'); | |
}); | |
manager.fire('myEventName'); | |
// pass parameters to event listeners | |
manager.addListener('myEventWithArguments', function(number) { | |
alert('the number is '+number); | |
}); | |
var random = Math.random(); | |
manager.fire('myEventWithArguments', [random]); | |
/* | |
* Use EventManager event instace to give your listeners more control | |
*/ | |
// cancel event | |
manager.addListener('myCancelledEvent', function(e) { | |
e.cancel(); | |
alert('this will be fired'); | |
}); | |
manager.addListener('myCancelledEvent', function(e) { | |
alert('this one will not'); | |
}); | |
manager.fire('myCancelledEvent'); | |
// remove a listener from within a listener | |
manager.addListener('myRemovedEvent', function(e) { | |
alert('event fired'); | |
e.remove(); | |
}); | |
//alert will happen | |
manager.fire('myRemovedEvent'); | |
// nothing will happen | |
manager.fire('myRemovedEvent'); | |
/* | |
* Let your own classes accept event handlers | |
*/ | |
function Person(name) { | |
this.name = name; | |
EventManager.eventify(this); | |
} | |
Person.prototype.changeName = function(name) { | |
this.name = name; | |
this.fire('namechanged', [name]); | |
} | |
var instance = new MyClass(); | |
instance.addListener('namechanged', function(name) { | |
alert('name changed to '+name); | |
}); | |
instance.changeName('Howard'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its very complex, please see this project https://github.com/uxitten/jetemit