As with all Backbone classes, you can create a new Router
subclass by using extend
, where the first argument provides the class's instance properties and methods and the second argument provides the static ones:
var MyRouter = Backbone.Router.extend({ // instance methods/properties go here }, { // static methods/properties go here );
Similar to Views
, Routers
take only a single options
argument when they are instantiated. This argument is completely optional, and the only real option it takes is the routes
option. As mentioned, once the Router
has been created, you will need to run Backbone.history.start()
before it can handle routes:
myRouter = new Backbone.Router({ routes: { 'foo': function() { // logic for the "/foo" or "#foo" route would go here } } }); Backbone.History.start(); // siteRouter won't work without this
18.226.34.197