Now, we are ready to start the router. The following is our Router
code for the router.js
file; we can start with this:
var Router = Backbone.Router.extend({ routes: { '': 'index' }, initialize: function (options) { this.podcasts = options.podcasts; this.nav = new NavView(); }, index: function () { layout.podcasts.show(new PodcastListView({ collection: this.podcasts })); } });
When the router is created, we'll accept a podcasts
collection. We'll also create our NavView
instance; remember, since the elements for this are already on the page, we don't have to append them. We're ready to take the root route with the index
method; when that happens, we'll use our layout.podcasts
region to show a PodcastListView
instance.
To use this router, let's add another script tag to the index.ejs
file:
<script> var r = new Router({ podcasts: new Podcasts(<%- podcasts %>) }); Backbone.history.start({ pushState: true }); </script...