1
votes

Currently I have an application where I created a layout with two regions. Out of two regions one is header and the other one is content region which gets populated by grid data.

I have 3 Itemviews to replace my content region. Now I want to know how do I store the values of the rows selected in the first itemview and display it in the third view. What I tried is I created a model object in layout and using that model in all the views to set and get as if it is a shared object and any view can access using layout.model.set() or layout.model.get().

Creating a model in layout is not a good approach it seems. What is the other way of sharing the data? If I use individual view specific model object then the data set in one view is not accessible to another view as the model gets created using new operator.

1

1 Answers

2
votes

I'm a big fan of a publish-subscribe pattern, and Marionette makes implementing this very easy with its Event Aggregator. The first view would send an event, let's say "viewname:selectedrows" and it would pass the specific models selected along with the event. It doesn't need to know who is subscribing to that event and it shouldn't care. It's simply exposing an interesting event. The second view can then access that data in its event handler.

The reason that I like an event-based architecture is that the views remain loosely-coupled. Instead of hard-coding references to a shared object, the view is completely self-contained. You could move it to any other part of the application and it would still work.