使用router.route时,不会触发页面加载时的初始主干路由

时间:2011-11-08 10:46:55

标签: javascript backbone.js frontend

这样做。

Admin.Routers.AppRouter = Backbone.Router.extend({
  // home() gets trigger when I visit http://example.com/admin
  routes: {
    "admin": "home"
  },

  initialize: function() {},

  home: function() {
    log("Home");
  }
});

这不起作用。

Admin.Routers.AppRouter = Backbone.Router.extend({
  initialize: function() {    
    var that = this;

    // home() DOES NOT get trigger when I visit http://example.com/admin
    this.route(/^\/admin$/, "home", function() {
      that.home();
    });
  },

  home: function() {
    log("Home");
  }
});

这是router.route的正确功能吗?

2 个答案:

答案 0 :(得分:0)

我假设您根据发布的示例网址使用{pushState: true}。你能试试以下吗?

this.route("admin", "home", function() {
      that.home();
    });

答案 1 :(得分:0)

Backbone.history.start({ silent: false });