Find all views of a particular type presently displayed in BackboneJS or BackboneLayout

52 views Asked by At

This project uses BackboneJS with BackboneLayout.

There is a rather complex hierarchy of views - four to five levels deep in some cases.

I have a situation where I would like one view to trigger a change in another view, but listeners are not an option. (results in too many listeners, kills memory).

Top -> X -> Y
Top -> B -> C -> D -> E

Top is the top level view, which has one X view , which has multiple Y views. Top view also contains one B view, which contain multiple B views, each of which contain multiple C views, each of which contain multiple D views, each of which contain multiple E views.

A change in a Y view needs to affect all E views that are presently rendered. This would be perfect for listeners, if not for the problem mentioned earlier. Thus I am looking for an alternative way to accomplish the same thing.

I am thinking of getting the Top view to find all of its children that are of type E, or something along those lines. Other solutions are welcome too.

Thanks.

1

There are 1 answers

0
AdamKG On

Yeah, I'd have Top maintain a list of all E instances - this would just be something in application code, might be in E.initialize(), or via your own methods for adding/removing nodes in your view heirarchy, if you have them - and it's own event handler that listens for change events on all Ys, and re-renders the Es. If it's prohibitively expensive to listen to Y changes, trigger a custom Y-change (or whatever) event on your Top from your application code in Y, and have Top listen to that event instead.

It's very easy to have the list of E instances get out of sync, but, well, that's why listeners exist. Tread carefully and make sure Top has hooks into every point down the B->C->D->E tree and knows what changes to make to it's (essentially denormalized) list of E's when there is a change.