I have an application that does CRUD for a Collection
of Models
. There is a DisplayView
for each model that is always visible. There is also an EditView
that is visible only when the associated DisplayView
is clicked on.
The DisplayView
and EditView
appear inside of different parent views. Right now I am using the "event aggregator" pattern to tell my application to render the EditView
when a DisplayView
is clicked. Pattern described here: http://lostechies.com/derickbailey/2011/07/19/references-routing-and-the-event-aggregator-coordinating-views-in-backbone-js/
When one of my DisplayView
is clicked it fires an event that the parent of the EditViews
listens to. When it receives this event it renders the appropriate EditView
, based on the model for which the event was fired.
This works well for most of my application, but is particularly cumbersome when I want to have the EditView
change position based on the absolute position of the related DisplayView
in my application. Rather than have the DisplayView
directly control the position of the EditView
, it triggers a "please reposition yourself to these coordinates" event. Such direct communication doesn't feel like something that should be broadcast to the entire application. I'm starting to wonder if for my case I should just have a reference to the appropriate EditView
as a property of each DisplayView
rather than decoupling them.
The problem, as I said, is that they are rendered inside of different parent views. The DisplayViews
get rendered in the HeaderView
while the the EditViews
get rendered in the ContentView
.
How do others handle situations like this? The EditView
in some ways belongs to the DisplayView
, but that does not match the way my application's DOM is structured. Assuming I do create a direct link between each EditView
and DisplayView
, how would I handle show/hide of the EditView
? Would the DisplayView
also need a reference to the ContentView
container, which it would render explicitly with the appropriate EditView
as a parameter?