Backbone.js路由方法没有被调用

时间:2013-06-30 03:54:11

标签: javascript backbone.js

我搜索了其他各种类似的StackOverflow问题,但似乎都没有提供解决方案。我有以下骨干路由​​器设置:

var AppRouter = Backbone.Router.extend({
routes: {
    ""           : "homeAction",
    "/portfolio" : "portfolioAction",
    "/about_us"  : "aboutUsAction",
    "/contact"   : "contactAction"
},
initialize: function  () {

},
homeAction: function () {
    alert("User has navigated home");
},
portfolioAction : function() {
    alert('user have navigated to portfolio');
},
servicesAction: function () {
    alert("User has navigated to services");
},
aboutUsAction: function () {
    alert("User has navigated to about us");
},
contactAction: function () {
    alert("User has navigated home");
},
requestQuoteAction: function () {
    alert("User has requested to submit a quote");
}
});

var app = new AppRouter();

$(function() {
    Backbone.history.start();
});

当我导航到mydomain.com/#时,homeAction路由会按预期调用。但是,如果我尝试导航到mydomain.com/#/portfolio,则没有任何反应。知道为什么不调用这个方法吗?

1 个答案:

答案 0 :(得分:1)

在路线开始时删除斜线应该有效。

var AppRouter = Backbone.Router.extend({
routes: {
    ""           : "homeAction",
    "portfolio" : "portfolioAction",
    "about_us"  : "aboutUsAction",
    "contact"   : "contactAction"
},
...

然后尝试去mydomain.com/#portfolio例如。