How Do You Switch Between Views in Backbone
I’m going to try to approach some of my future articles as more of a question/answer setup so they don’t turn into these sprawling tutorials. Today I want to focus on moving between views in Backbone.js. I’m starting with some very simple templates and three views: LeftView, MiddleView, RightView. To do this quickly we’ll make it so each view is essentially a big button which, when clicked on, should animate to the middle of the screen.
Here’s what one of my templates looks like:
One of the first thing I’m noticing is that all of my templates seem to be wrapped in an extra div. Since this extra div is block displayed I can’t get my items to line up next to each other… Oh! The problem is because I haven’t specified a tagName for my views. I think I can actually do everything in the View declaration without needing a template.
That should create our view for us with the proper tag, class and id attributes. My containers are just colored squares so I don’t need to populate them with any content. If I did want to use this approach I could add more content like this:
Or I could render a template. Again for our purposes we just want to move some colored blocks around so the first approach is sufficient.
Here is our most basic Router
showing how to add the views to stage. Since we aren’t using a template we can just call the regular render function and append the returned element to the DOM.
Here are my very simple styles:
We should now have a very simple horizontal layout.
Composite Views
Well I’d like to center my views in the middle of the screen but moving each item individually is going to be pretty challenging. I think the better idea would be to wrap my views in a containing view which can then be easily centered on screen.
Here’s what that looks like:
Our Sections view is going to contain our 3 subordinate views. When it gets added to the DOM, initialize
will run and create our subviews. I’ve also defined a method setInitialPosition
which centers our view on screen. Tomorrow I’ll replace this with an event handler that fires whenever our element is added to the DOM. For now I’m too lazy to look up the supported events :D
The sections view is absolutely positioned and it’s width and height are explicitly defined in the css. In the short term here’s how we’ve updated things:
I wanted to give each view a 50px margin on each side so in the short term all these values are hard coded. I’ll think about how to make things more dynamic.
Let’s listen to when the user clicks on a view. When we hear that we’ll animate the whole sections container over so that view is centered on screen.
We’ll need to add an events hash to our Sections view. Since all of our children implement the same .container
class we may as well listen for a click on that.
In our handler, onChildClicked
, we’ll figure out which child was actually clicked and then animate ourselves accordingly. Here’s the entire object for your reference with the handler at the bottom.
You should follow me on Twitter here.
- Mood: Laggy, Pensive
- Sleep: 5
- Hunger: 0
- Coffee: 0