0

right now im diving into canjs, it looks like a great mvc framework. I'm having troubles with the router. I'm using wamp for making my tests in localhost. So the problem is the following: when I enter localhost the initial url in the browser is "localhost" which matches my first case in the router which handles the empty urls. Problem is that I'm doing a window.location.hast to redirect the user, but after I do this it doesn't gets routed so this is the part i need help with. My router code is as follows:

$(function() {
'use strict';
var Routing = can.Control({
    defaults: {}
}, {
    'init': function() {
        console.log('router init');
    },
    //default router
    'route' : function(){
        console.log('default route');
        window.location.hash= "#!dashboard/london/";//doesnt works like this
        //can.route.attr('location','london');//like this it works!!!
    },
    //route i cant match!
    'dashboard/:location/ route' : function(data){
        console.log(data.location);
    }
});

can.route.ready(false);
new Routing($('body'));
can.route.ready(true);

});

ramblinjan
  • 6,578
  • 3
  • 30
  • 38
rdk1992
  • 406
  • 1
  • 5
  • 20

1 Answers1

2

I created a Fiddle with the latest version (which only needs to call can.route.ready() to start routing) running your code and I get

router init
default route
london

Which should be what you're expecting.

Daff
  • 43,734
  • 9
  • 106
  • 120
  • it also ended up being the can.custom.js library. I downloaded another version and it started routing correctly. Weird stuff – rdk1992 Oct 29 '13 at 22:40
  • Would you mind letting me know what configuration you downloaded? Maybe there is an issue we're not aware of. – Daff Oct 30 '13 at 13:43
  • i have this link `http://localhost/canjs/#!active` and when i refresh this link, it does not call the `'active route': function () {` how can i invoke this function if user refresh the page and the hash matches one of my route? – Khawer Zeshan May 28 '14 at 07:52
  • With 2.0+ you need to call `can.route.ready()` after you set up the listener. Then it should work. – Daff May 28 '14 at 17:07
  • been some time since I asked this question but wanted to point out something out: @Daff when I download canjs from the official site: http://canjs.com/download.html If I use the code on the CDN link everything works great, now if I use customize build which downloads a can.custom.js file the routing stops working. Btw kudos on a great library been using it for a while now :) – rdk1992 Dec 11 '14 at 14:21
  • Probably because you are including pushstate (http://canjs.com/docs/can.route.pushstate.html) which changes the way routing is done. – Daff Dec 11 '14 at 14:51