2013-01-06 68 views
0

我无法触发一个“刽子手”功能,当我去的路线整合轨道和骨干路由

的application.js

var init = function(){ 
    console.log("init called");  #logged 
    var router = new Router(); 
    console.log(router); 
    Backbone.history.start(); 
}; 

当我检查在控制台中的“路由器”的变量。日志(路由器),它显示了刽子手功能的路由器对象

child 
__proto__: ctor 
constructor: function(){ return parent.apply(this, arguments); } 
hangman: function() { 
routes: Object 
/hangman: "hangman" 
__proto__: Object 
__proto__: Object 

但是上创建的,当我浏览到localhost:3000 /刽子手,警报是没有得到调用。你能提出我可能做错了什么吗?我在Rails应用程序中使用主干。我不太清楚Backbone如何理解Rails路由,因为我的rails路由器文件中定义了localhost:3000/hangman。我想我可能必须做localhost:3000/hangman /#hangman才能激活骨干路由(铁路路由无关紧要),但这也不起作用。

router.js

$(function() { 

    window.Router = Backbone.Router.extend({ 

    routes: { 
    '/hangman' : 'hangman' 
    }, 

    hangman: function() { 
     console.log("hangman called");  #never logged 
     alert("hangman"); 
    } 

    }); 

}); 

回答

0

fine Router#extend manual

延长Backbone.Router.extend(properties, [classProperties])

[...]请注意,你要避免使用前导斜杠您的路线定义:

你可能会感兴趣的pushState option还有:

开始Backbone.history.start([options])
[...]
以表明您想使用HTML5 pushState支持你的应用程序,使用Backbone.history.start({pushState: true})

所以首先解决您的路线:

routes: { 
    'hangman' : 'hangman' // No more leading slash. 
} 

然后用pushState: true获得/hangman(而不是/#hangman)工作:

Backbone.history.start({pushState的:真}) ;

+1

只要我看到“好”这个词,我就知道谁在回答这个问题。顺便说一句,你必须具有一定的智力和经验来应用手册中的信息,并且我在 – Leahcim

+0

上“mu(ch)太短”这是我最喜欢的开篇之一:) –

+0

希望你也看到了我的编辑。这可能是我最好的双关语之一 – Leahcim