javascripturl-routingcanjscanjs-routing

Canjs Not Routing


so this bug is killing me as I have no clue what's going on, I updated my canjs version to the latest which is currently 2.0.4 with jquery, and the router stopped working. Is not routing anything, I try with window.location.hash and can.route.attr and is not routing. The thing that bugs me is that with the old version I had was working perfectly. Here is the code

var Router = can.Control({
    'init': function() {
    },
    'route' : function(){
        window.location.hash = '#!dashboard';
    },
    'dashboard route': function() {
        console.log('dashboard')
    }
});

$(document).ready(function() {
    can.route.ready(false);
    new Router($('body'));
    can.route.ready(true);
});

Solution

  • FYI, the latest CanJS is 2.0.5, released yesterday.

    can.route.ready() doesn't work like it used to. can.route.ready(true) in fact does nothing, in order to prevent multiple route setups in legacy code. This explains somewhat why your code isn't initializing that first jump to #!dashboard, since you would need to init the Router controller before the call to ready.

    Better to remove the first call to ready and take out the argument from the second. I have a demo of this at http://jsfiddle.net/air_hadoken/5maLu/1/ -- click Run and you will see evidence of each route listener in your controller firing.