The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

howdy99 (3) [Avatar] Offline
#1
I'm using the Observer pattern, specifically the eventRouter.js file from chapter 4. It works great for functions that do not require any parameters/arguments. How do I use the Observer pattern when I need to pass parameters with the function?

Thanks,
Ed
kurinosuke (156) [Avatar] Offline
#2
Re: Observer Pattern - how to pass parameters to addListener
The call() function in jsEvent.EventRouter.prototype.notify can accept any number of arguments. Pass it whatever you want. But I'm not sure this reflects the spirit of listeners.
howdy99 (3) [Avatar] Offline
#3
Re: Observer Pattern - how to pass parameters to addListener
Okay, I'm new to using patterns for Javascript, so I am trying to understand what you mean by "spirit of listeners".

What I've had to do so far is re-write my functions so that, instead of passing arguments into the function (like, say, the value of the element that was clicked), the functions figure this out on their own via the event object. Am I on the right track? Should I continue to eliminate the need for passing arguments into the functions? Is this what you mean by "spirit of listeners"?
kurinosuke (156) [Avatar] Offline
#4
Re: Observer Pattern - how to pass parameters to addListener
Sorry for being so unclear. Yes, by "spirit of listeners", I meant that you should pass only an event object, which is supposed to hold all the necessary information. I come from a J2EE background, where listeners only receive one event object, like ServletContextListener . If you know Java, you could also refer to the Observer interface, where instances receive only one Object argument when they have been notified. I think it's better to pass only one object to the target.
howdy99 (3) [Avatar] Offline
#5
Re: Observer Pattern - how to pass parameters to addListener
Excellent! Thanks!