I'm wanting to use custom events for Javascript
, and the truth is that I try to find something but either I can not find the indicated information or I do not understand it, so it occurred to me to do my eventManager
, quite truthful but it is what you can do at the moment to stop braking, the only problem I have is that I do not know how to execute my handler inside the object EventManager
.
Here I leave the code to see if someone can help me.
My problem is in this line, it's clearly wrong but I do not know how to do it
this._events[i].handler (this._events[i].args)
.
var EventCustom=function() {
this.eventName;
this.handler;
this.args;
};
var EventManager=function(siteController) {
this.siteController=siteController;
// es un array de objetos de tipo EventCustom
this._events = [];
};
EventManager.EVENT_ON_USER_LOGIN = 'EVENT_ON_USER_LOGIN';
EventManager.prototype.addEventListener = function (evnt, handler,args)
{
var tmpEvent = new EventCustom();
tmpEvent.eventName = evnt;
tmpEvent.handler = handler;
tmpEvent.args = args;
this._events.push(tmpEvent);
//alert("addEventListener:"+this._events[0].eventName);
//handler
}
EventManager.prototype.dispatchEvent = function (evnt, args)
{
for(var i=0; i< this._events.length; i++)
{
if(this._events[i].eventName == evnt)
{
**// aca deberia ejecutar el handler pero no me funciona
//this._events[i].handler (this._events[i].args)//**
}
}
}
var pruebaFn = function()
{
alert("prueba");
}
var eventManager = new EventManager(this);
eventManager.addEventListener(EventManager.EVENT_ON_USER_LOGIN,pruebaFn);
eventManager.dispatchEvent(EventManager.EVENT_ON_USER_LOGIN);
If you also want to recommend a better way to do it, great. The truth is that I am setting up an entire system in Javascript
and I want to pass certain calls to events since it is much more ordered.