I have written an observer in javascript. You can trigger Events like this:
ClassThatExtendsEvent.triggerEvent('EventName', arg1, arg2, ..., argn)
There is a variable number of arguments, with the first argument being the event name. I do as well have subscribers, that are saved in a private property of my event class. They are getting called like this:
Event.prototype.triggerEvent = function() {
var callbacks = this._events[eventName];
if(callbacks !== undefined) {
for (var i = 0, l = callbacks.length; i < l; i++) {
// I do not need the first argument here!!!
callbacks[i].apply(callbacks[i], arguments);
}
}
}
However, the subscribers do know the event name, hence it could be ommitted. But I do not know, how to remove one argument from the list (as it's not an array but seems to be some kind of object).
I also need to readd the event name, once it's been deleted, because after notfiying the direct subscribers, a global event system kicks in. This global event system needs the name.
Hence the entire function looks like this:
Event.prototype.triggerEvent = function() {
// First we deliver to direct subscribers
var callbacks = this._events[eventName];
if(callbacks !== undefined) {
for (var i = 0, l = callbacks.length; i < l; i++) {
// I do NOT need the first argument here!!!
callbacks[i].apply(callbacks[i], arguments);
}
}
// Now we broadcast the event to the entire system
// I DO need the first argument here!!!
Facade.getInstance().broadcastEvent.apply(Facade.getInstance(), arguments);
}
How would you implement this?