Search code examples
javascriptdesign-patternsbackbone.jscomposite

What object should be responsible for viewtransitions in composite pattern?


enter image description here

now that you have seen my awesome uml diagram I can go on with the real stuff.

Lets say a leaf in the left sidebar receives an action, this action wants to change something outside the scope who is responsible for changing the mainview?

The containercomposite, a seperate viewmanager, the sidebarcomposite?

It might be a lite vague and/or subjective, but I would like to know the most general way to do this when it goes to deeper levels of nesting.


Solution

  • There is no one true answer to this:

    One solution is to extend the event-model provided by Backbone to create an application level event aggregator (a great blog post about this technique here) and to toss events from the composite view to the event aggregator and then having MainView listening to the event aggregator for the suitable events and changing itself accordingly.

    Another solution would be to bind your compview's elements to suitable models and collections and having actions of your compview modify those collections and elements and then having your MainView tied to those models and collections by listening to events they emit. A bit simialiar to the solution above.

    Yet another solution could be to have some kind of controller object that has methods that modify the MainView and have the compview call the controller's methods whenever the MainView needs to be changed.

    I think there are numerous other ways to accomplish communication between separate views and elements, but these three are good and at least on some level manage to maintain a decent level of separation in your code. Of course you can go for a mix of these like using event aggregation for application level communication but controllers for the single module level, but that's just an idea.