Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching views in backbone for navigating between pages - whats the right way?

Tags:

I have made a decision to change my application routing to client side rather than server side. This means I will need a way to switch views in and out of the page as the user navigates the site. The only thing I could find that attempts to document this is this article: How to switch views using Backbone.js

I see how this would work, but I don't think is a great way of doing it. I want to keep my views - as that's the whole point right? To have separate views for the distinguishable parts of your application? I think having one big "ContentView" and then just pulling stuff into it and re-rendering is a bit crude and bypasses all the cool modularisation you can do otherwise.

So what is the best way to go about it? Ideally I want a function similar to what is documented in the aforementioned article, but takes a backbone view as its argument.

like image 578
Adam Thomas Avatar asked Feb 12 '12 17:02

Adam Thomas


People also ask

Can I use navigation links between views in an app?

If you end up declaring a navigation view on multiple views, you will end up with crazy nested navigation links that do not look good and make navigating through your app difficult. After you have created a navigation view, you are then in the clear to begin using navigation links to navigate between views.

How do I style the navigation view?

Similar to styling the navigation links, you are also able to style the navigation view itself. A common use for styling the navigation view is adding a navigationBarTitle. You can see an example of this below:

What are the keyboard shortcuts for working with pages and side notes?

ALT + END = Go to first page in section. CTRL + TAB = Go to next section. CTRL + SHIFT + TAB = Go to previous section. Check out the whole list of keyboard shortcuts for “Working with Pages and Side Notes” provided by Microsoft Office Support here.

How do I go to the next page in a page?

ALT + HOME = Go to first page in section. ALT + END = Go to first page in section. CTRL + TAB = Go to next section. CTRL + SHIFT + TAB = Go to previous section.


1 Answers

I've written a few articles on this subject:

http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

and an newer one that takes the idea from this post and formalizes it more:

http://lostechies.com/derickbailey/2011/12/12/composite-js-apps-regions-and-region-managers/

re-using views is actually an anti-pattern in most cases. there's usually a lot of additional code and extra cruft involved in holding on to view instances, to get them to re-attach themselves to the DOM and handle registered DOM events correctly. Additionally, you run the risk of memory leaks (which my first article talks about) and destroying your application performance by using up too much memory.

If your views are using an "expensive" resource, you should cache that resource outside of your views and re-use it. Your views should be cheap and fast to create, render, display, and destroy.

like image 62
Derick Bailey Avatar answered Nov 15 '22 11:11

Derick Bailey