I have an event/listener manager that has this function:
var addListener = function(event, listener) {
myListeners[event].push(listener); //assume this code works
}
But now I need to change it so that it looks like this:
var addListener = function(event, listener, fireFirst) {
if(fireFirst) {
myListenersToFireFirst[event].push(listener);
} else {
myListenersToFireSecond[event].push(listener);
}
}
This is so that when the fireEvent function is called, it will fire the listeners in the myListenersToFireFirst array first, then the listeners in the second array.
So it will look something like this:
var fireEvent = function(event) {
var firstListeners = myListenersToFireFirst[event];
//for each listener in firstListeners, call `apply` on it
var secondListeners = myListenersToFireSecond[event];
//for each listener in secondListeners, call `apply` on it
}
Is this the best way to accomplish this in JavaScript? Is there a more elegant way of achieving this priority list of listener-event firing?
maybe its a better way then mine.. but that is a specific way, i mean you have to insert new handlers in the block. This is a more generic tool, however it seems to have application on your case.
i suggest this:
with this code you can append/prepend functions to each other to form a kind of chain, its usefull to add another listener or to track a functions usage, or even do adapt code with minimal impact.
some usage