Reputation: 4778
What is the difference between these two methods of handling routes?
var Router = Backbone.Router.extend({
routes: {'home': 'showHome},
showHome: function() {//do whatever}
});
and
var Router = Backbone.Router.extend({
routes: {'home': 'showHome},
initialize: function() {
var router = new Router();
router.on('route:showHome', function(){//do something});
}
});
Currently I have it the second way. It works fine and I get to the right places..
My router is now set up properly(I think) but my routes are still acting weird(as follows).
The only problem I am having is that when it goes to change a route the address bar flickers between multiple different routes before it lands on the right page (always gets to the right place).
I am changing pages be using window.location = '#/route';
inside of jQuery listeners inside of my views.
$('#right_arrow').live('click', {view: that}, this.rightArrow);
...
rightArrow: function(e){
var that = e.data.view;
if(typeof window.easyUserData.fbResponse.authResponse === 'undefined') {
// Not logged in
window.location = '#/login';
} else {
// Logged in
window.location = '#/notes/right';
}
return false;
}
I have been trying to debug and what i have found when i set the breakpoint at window.location = '#/notes/right';
:
Why is it behaving like this? Is it related to how I have my router set up?
Upvotes: 0
Views: 97
Reputation: 9597
Your code is instantiating a new Router in the initialize function, which is what is called when that particular object is instantiated to begin with. So essentially, you're creating an unnecessary router.
The code in the first example is correct. Then you simply do:
var myRouter = new Router();
Upvotes: 1