【问题标题】:backbone router does not pick up the function骨干路由器不接功能
【发布时间】:2012-11-11 19:33:41
【问题描述】:

我尝试使用带有 pushstate 的骨干:

$(document).ready(function(){
    var App = Backbone.Router.extend({
        routes: {
            "/":            "homepage",
            "/questions":   "questions"
        },
        homepage: function() {
            console.log('why');
        },
        questions: function() {
            console.log('this is not working?');
        }
    });
    var the_app = new App();
    $("a").click(function(ev) {
        the_app.navigate( $(this).attr("href"), true);
        return false;
    });
    Backbone.history.start({pushState: true});
})

问题是,每当我点击一个这样的链接时:

<a href="/"> home </a>
<a href="/questions"> questions</a>
<a href="/justlink"> just link</a>

浏览器中的位置会更改而无需重新加载,但与它关联并且我已经在路由中定义的功能不会执行。 知道我做错了什么吗?

【问题讨论】:

    标签: backbone.js backbone-routing


    【解决方案1】:

    来自fine manual

    扩展 Backbone.Router.extend(properties, [classProperties])

    [...] 请注意,您需要避免在路由定义中使用前导斜杠:

    您希望您的routes 看起来像这样:

    routes: {
        "":            "homepage",
        "questions":   "questions"
    }
    

    演示:http://jsfiddle.net/ambiguous/ybPcg/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      相关资源
      最近更新 更多