I got several different views which are mostly built up like (layout-component, layout, main view).
When I now switch between different views the whole page has to get rerendered.
Wouldn’t it be better to cache at least the layout view in “window” and reload them?
Something like a singleton pattern for backbone views?
How do I do this?
Is a simple:
window.MainLayoutView || window.MainLayoutView = new MainLayoutView({ el: 'div.main' });
enough?
Is there anything else I have to think about?
Yes, there is more to worry about. If you do something like this:
you’ll find that all your events inside
window.MainLayoutare gone. If you want to cache the instantiated view and swap it in and out, you’re going to have to arrangedelegateEventscalls to rebind all the events all the way down the view hierarchy.Compare the behavior of these two examples and you’ll see the problem:
delegateEventscalls).delegateEventscalls).Generally you don’t bother trying to cache views, just
removethem and recreate them as needed.