2014-01-12 66 views
1

我想使用this tuto,但不爲我工作JavaScript框架canJs control.route

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

感謝幫助

回答

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/

+0

是, 有用 !謝謝你的幫助Daff – Phane