eventManager anonymous obj

pull/1/head
yann300 8 years ago
parent 01d700dc7c
commit a19c003b25
  1. 17
      src/lib/eventManager.js

@ -2,6 +2,7 @@
function eventManager () { function eventManager () {
this.registered = {} this.registered = {}
this.anonymous = {}
} }
/* /*
@ -13,15 +14,16 @@ function eventManager () {
* @param {Func} func - function of the listenners that will be executed * @param {Func} func - function of the listenners that will be executed
*/ */
eventManager.prototype.unregister = function (eventName, obj, func) { eventManager.prototype.unregister = function (eventName, obj, func) {
if (!this.registered[eventName]) {
return
}
if (obj instanceof Function) { if (obj instanceof Function) {
func = obj func = obj
obj = {} obj = this.anonymous
} }
for (var reg in this.registered[eventName]) { for (var reg in this.registered[eventName]) {
if (this.registered[eventName][reg] && if (this.registered[eventName][reg].obj === obj && this.registered[eventName][reg].func === func) {
this.registered[eventName][reg].obj === obj && (!func || this.registered[eventName][reg].func === func)) {
this.registered[eventName].splice(reg, 1) this.registered[eventName].splice(reg, 1)
return
} }
} }
} }
@ -40,7 +42,7 @@ eventManager.prototype.register = function (eventName, obj, func) {
} }
if (obj instanceof Function) { if (obj instanceof Function) {
func = obj func = obj
obj = {} obj = this.anonymous
} }
this.registered[eventName].push({ this.registered[eventName].push({
obj: obj, obj: obj,
@ -56,9 +58,12 @@ eventManager.prototype.register = function (eventName, obj, func) {
* @param {Array}j - argument that will be passed to the exectued function. * @param {Array}j - argument that will be passed to the exectued function.
*/ */
eventManager.prototype.trigger = function (eventName, args) { eventManager.prototype.trigger = function (eventName, args) {
if (!this.registered[eventName]) {
return
}
for (var listener in this.registered[eventName]) { for (var listener in this.registered[eventName]) {
var l = this.registered[eventName][listener] var l = this.registered[eventName][listener]
l.func.apply(l.obj, args) l.func.apply(l.obj === this.anonymous ? {} : l.obj, args)
} }
} }

Loading…
Cancel
Save