简体   繁体   English

Backbone.js无法访问路由器功能?

[英]Backbone.js unable to access the router functions?

I have a simple implementation of Backbone.js which goes like this - 我有一个Backbone.js的简单实现,它像这样-

$(document).ready(function(){
    Workspace = Backbone.Router.extend({
    routes: {
    "/getAcademics": "academics"
        }, 

    academics: function(){
            alert("ok");
    $("#content").append("<div>Academics</div>");
    }
    }); 
var myWorkspace = new Workspace;
});

The link in the body is given as - 正文中的链接为-

     <a href="#/getAcademics">Academics</a>

Though the router routes to the link #/getAcademics but it does not execute the function academics. 虽然路由器路由到链接#/ getAcademics,但它不执行功能Academic。 Any clues as how to rectify this. 有关如何纠正此问题的任何线索。 Am I missing something else. 我还想念其他东西吗?

Thanks 谢谢

After you instantiate your router, and before you attempt to use any of its routes, you need to call Backbone.history.start(). 实例化路由器后,在尝试使用其任何路由之前,需要调用Backbone.history.start()。 This will setup a listener for hash changes and call the correct route functions when needed. 这将为哈希更改设置一个侦听器,并在需要时调用正确的路由函数。

You can find more information here: http://documentcloud.github.com/backbone/#History 您可以在这里找到更多信息: http : //documentcloud.github.com/backbone/#History

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM