| > This can work pretty good up to certain level but after that this can go out of hand pretty quickly. The cascading effect will become hard to debug. Totally agree, we recognized this before we began using them. They are used for common elements between modules, and we don't hold a lot of data with them. >At least it is at one place to modify and debug. This is loosely coupled. You can easily decide how to react based upon messages. Very easy to introduce new workflows or use cases. Maybe. With pub/subs you still need to go hunting for a rogue publish. With my session example, we use the session object to display the user's name in various places. It's easy to use the session object to do this; I can just stick it in the view. With pub/subs, I would have to listen for the publish and then update the view, and I would have to do this in many modules. Turns a 0 line operation into a 5-line one. > Global objects are evil Not true at all! A session is a global thing, isn't it? Why wouldn't it belong in the global context? Edicts like "global objects are evil" end up paralyzing your development and you will sometimes have to do even more evil things to get around them. |
>With my session example, we use the session object to display the user's name in various places. It's easy to use the session object to do this; I can just stick it in the view.
Correct. No need for pub\sub here since data is coming from server and we are not manipulating it. Pub\sub is required in scenarios where actions are taking place on client side like user registration, forgot pwd or ordering a product etc
> A session is a global thing, isn't it? Why wouldn't it belong in the global context?
Absolutely. Session is more of a read-only object after once set on the server. I know some people use it to store objects and that is not right.
A bad example of a global object would be
var settings =
{ showCheckoutbutton: false, Ordertotal: 200, calculatedtax: false, showOffers: true, ShowTopbar: false }
I constructed this hypothetical object just to give an idea about an evil entity. Some developers would set certain properties in certain functions. It would definitely show zombie behaviour at certain times.
I enjoyed our conversation. Thanks for being so nice with your response.