A model listening to other models? Yuck, that's just wrong. In an MVC application, only controllers should update models. Just because Backbone lets you listen to model events from anywhere doesn't mean it's a good idea. IMHO Application events should simply bubble up through the DOM, and get caught by appropriate controllers (Backbone.View class in backbone parlance) that then modify the appropriate models. If need…
Backbone is considered a high-quality javascript project, written by a very talented and experienced developer. Could you explain what you do differently in your MVC framework to achieve those gains?
Avoiding Event Chains in Single Page Applications
61–64 of 64 posts
Re: Avoiding Event Chains in Single Page Applications
#62Maybe the OP is just giving a bad example, or maybe I'm missing something - but one thing I really don't like in his example is that model B's 'waitFor' call needs to know about Model A, and that model A has an appDispatch. This kind of tightly coupled code isn't going to do anything useful for your codebase if you have a large application. It might make debugging easier right now because things are more explicit, but it will make you less flexible in the long term. It also doesn't solve the problem that you still need to understand that A has to finish before B can start - which I suppose most people would simply solve with an additional event. The OP does mention this in the article, but I'm not convinced that the trade off is worth it here. If I'm to explicitly state dependencies within the event handling code, then that means that any time I want to change how things handle events, I've got to remember exactly which of my dependents reference me. This isn't helpful - and in fact it may be more painful than the current situation.
I guess my main problem here is that the benefits this brings just aren't enough (in my view) to offset the potential pain. To me, it kinda just feels like cutting off your nose to spite your face. You might make one area a bit easier to debug, but you lose out in other areas too.
On the surface the code may look more sensible and easier to think about, but in practicality I'm not convinced that this won't introduce additional pain later on.
Re: Avoiding Event Chains in Single Page Applications
#63Earlier quoted context omitted.
Well, right now, he has to explicitly listen for one event in two places, and annotate the order correctly. With two separate events types, he would only have to listen for the events.
Nope, he doesn't have to annotate the order correctly. Except if you mean locally, in which case it's trivial.
A: listen for X
B: listen for X
B: execute after A
and A: listen for X
B: listen for Y