I have following piece of code from my Backbone project:
App.Controllers.Test = Backbone.Router.extend({
routes: {
'test': 'test',
'help': 'help'
},
help: function() {
console.log('help');
},
test: function() {
console.log('test');
},
initialize: function() {
console.log('init');
}
});
// ...
new App.Controllers.Test()
But all I ever see in my console is init even if call the url with #test or #help
Does anybody know what is missing?
Your initialize function will always run first, so your router is being instantiated, but perhaps you haven’t invoked History, after you invoke your router you need a line of code like:
The pushState option is if you are using newer browsers that have the history api, since you’re calling your routes with a hash, you might not need pushState.
If your router is assigned to the variable routes, your code may look like this: