Today, the second part of exploring the Backbone.js secrets.
Backbone.js tutorial
After talking about the basics and key aspects, such as models and collections, we will focus on next — Router, View and Events.
Routing — Backbone.Router
For what would be our application without the possibility of interaction?
Backbone.Router provides routing methods for client-side pages, connections with actions and events. There is also “graceful fallback” solution for browsers without History API support.
Example:
MyRouter = Backbone.Router.extend({ routes: {'hello' : 'sayHello'}, sayHello: function() { console.log('Saying hello'); } }); var router = new MyRouter(); Backbone.history.start();
So we simply define a route with associated function to execute, and then we initialize the Backbone.history:
Backbone.history.start();
That’s it — we can test our code.