- Overview
- Transcript
5.1 Creating Routers
Routers are the way we can manage the states of our application: we probably don't want to display all our views on the page at once, so we can use a router to manage it all.
1.Getting Started3 lessons, 13:56
1.1Introduction01:04
1.2Backbone Data Flow03:16
1.3Backbone Classes09:36
2.Backbone Models7 lessons, 38:03
2.1Creating Models03:19
2.2Working With Model Attributes07:44
2.3Validating Models04:04
2.4Persisting Models to the Server08:35
2.5Using Model IDs03:43
2.6Listening for Model Events07:47
2.7Creating Model Methods02:51
3.Backbone Collections6 lessons, 17:52
3.1Creating Collections01:10
3.2Managing Collection Contents03:14
3.3Syncing Collections04:18
3.4Using Collection Methods03:14
3.5Sorting Collections02:42
3.6Listening for Collection Events03:14
4.Backbone Views4 lessons, 26:08
4.1Creating Views03:18
4.2Rendering Views07:25
4.3Managing DOM Events10:42
4.4Persisting Data With Views04:43
5.Backbone Routers3 lessons, 11:19
5.1Creating Routers01:59
5.2Adding Routes04:26
5.3Navigating With Links04:54
6.Conclusion1 lesson, 01:36
6.1Conclusion01:36
5.1 Creating Routers
In this lesson we will begin discussing routers and backbone. The purpose of a router is to allow to create pages or states of your application. One of the issues of web applications that are front end heavy is that since there's no longer a need for the page to refresh, you can build an entire application that is hosted at a single URL, and so every single page has the same URL. Routers allow you to change that. They allow you to give different parts of your web application different routes. And that way, users will be able to say, bookmark different pages of our application. So in our application here, let's begin with a basic router class. I'm gonna create an AppRouter. And I'll be completely honest with you, you can really call your router whatever you want. Often, I'll just call mine Router, but AppRouter is good too. Now, you can actually, use multiple routers with your application if you want to divide the routes that you create, into different sets. And if that's the case, you may want to give it a more specific name. For example, I might call it BookRouter and let's go with that for now. Now just like all of our other Backbone components, we create a router by extending backbone.router. And of course you'll rarely have a router that has nothing inside of it. So, in the next lesson we're going to look at filling in this router. Instantiating a router, is as simple as saying new BookRouter for example. And just by instantiating a router, you have enabled it. So the next step after this would be to do Backbone.history.start. And go ahead and start up history. Starting with the history means that backbone will take note of route changes and this way you'll be able to move forward and backwards throughout your application using the router. Now if we just start up Backbone history like this, the routes that we create will use hash bangs. However, I would rather use push state, so we will pass an options object to start and say pushState equals true. And this way we can use push state routes. So this is the initial way to set up a router. In the next lesson we will look at creating specific routes.