Backbone Router

In notebook:
FrontEndMasters BackboneJS
Created at:
2016-06-10
Updated:
2016-06-10
Tags:
libraries JavaScript
maps of routes (regular expressions):
  var Router = Backbone.Router.extend({
    routes: {
        "songs": "showSongs",
        "songs/:id": "showSong",
        "*args": "index"
    },
    showSong: function () {
        // songs route
    },
    showSongs: function () {
        // songs/id
    },
    index: function(args) {
        
    }
})
There are also some special characters (​songs/:id​)If you organise everything around routes - you would often recreate things. 

Make sure you take account it's statelessness. 

.navigate

Can ​navigate(fragment, [options])​ to change the route. 
  • ​{trigger: true}​ – call routing method
  • ​{replace: true }​ – do not create history entry
"be default changing the route does not trigger any change event on the router" 
It's just a passive way to add another item to the routing history. Usually the route changing is a byproduct of something changing in the data, and/or a view update. So you don't want to trigger a navigation, just update the routes history.

Backbone.histroy.start

The Backbone.history has to be set up and started manually. you can specify ​{pushState: true}​ to use HTML5 Api (by the default it uses hash state monitoring).
Push state is more complex to implement, because you have to mirror each state (potential URL combination) on the server as well.