Javascript框架可以控制.route

时间:2014-01-12 14:51:20

标签: javascript canjs canjs-routing

我想使用this tuto,但不适合我

    $(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);

    });

我使用window.location.hash = "!#completed"<a href="#!active">Show Active</a> 控制台中不显示任何消息。

我使用canjs 2.0.4和JQuery

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您必须致电can.route.ready()初始化路由:

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();

这是小提琴:http://jsfiddle.net/DeGR5/