I am trying to write an object relationship that follows an observer pattern, where the observer takes care of a particular set of events that occur on this subject.
I'm not sure that this is a 100% standard, but the way I built it, these event objects are defined in the observer with custom callbacks that will fire when an event occurs. When an event occurs on this issue, it passes through all its observers and watches who watches this event. If he finds observers watching this event, he calls the observer event callback.
Since I want my observers to be able to add and remove, on the fly, the events that need to be watched, I need the event object to be able to delete itself after the callback starts ... let's say for an instance, if my observer only wants to respond to the event once, and then no longer track it.
This seems like a good plan, but I know that the JavaScript object cannot call delete () itself.
I'm just curious that someone else there has come across this and came up with an effective solution.
My only thought was that I could pass a reference to the parent observer object, a child event on it, and then when the callback happens, I could call the method inside the parent ... something like removeEvent(this) passing the event itself to this function. The removeEvent function can then splice an event from this array of events. The only difficult problem is finding the location of this event object in the array. (also accepting suggestions on this subject, thank you!).
Thanks in advance for your help!
source share