javascriptcanjscanjs-routing

Javascript framework canJs control.route


I would like use this tuto, but doesn't work for me

    $(function() {

    Router = can.Control({

      "completed route" : function(){
        console.log("the hash is #!completed")
      },
      "active route" : function(){
        console.log("the hash is #!active")
      },
      "project/create" : function(){
        console.log("the hash is #!project/create")
      },
      "route" : function(){
        console.log("empty hash")
      }
    });

    // make sure to initialize the Control
    new Router(document);

    });

I use window.location.hash = "!#completed" or <a href="#!active">Show Active</a> no message is displayed in console.

I use canjs 2.0.4 with JQuery

Thank for help


Solution

  • You have to call can.route.ready() to initialize the routing:

    Router = can.Control({
      "completed route" : function(){
        console.log("the hash is #!completed")
      },
      "active route" : function(){
        console.log("the hash is #!active")
      },
      "route" : function(){
        console.log("empty hash")
      }
    });
    
    // make sure to initialize the Control
    new Router(document);
    
    can.route.ready();
    

    Here is the Fiddle: http://jsfiddle.net/DeGR5/