如何重定向到 Backbone router inside some other function

How to redirect to Backbone router inside some other function

快速提问。我有重定向到应用程序不同部分的路由器。其中之一是例如游戏,一个是菜单等。

我现在想要 javascript 中的一个简单功能,例如重定向到 #game 或 #menu。我知道我可以将 Model 与 .set 函数一起使用,但它仍然会以同样的方式工作吗?

这是我要调用此重定向的文件:

var Controls = BaseView.extend({
    id: "controls",
    template: require('./controls.tpl'),
    events: {
      'click #game': 'gotoGame',
      'click #menu': 'gotoMenu'
    },

    gotoGame: function() {
        //Redirect to #game
    }
});

路由器在其他文件中 game_router 像这样:

var Router = BackboneRouteControl.extend({
    routes: {
        'menu': 'menu#index',
        'game': 'game#show'
    }
})

那么我如何才能在一个函数中重定向到 "someaddress"/#game?

使用Router.navigate。在你的情况下,它应该是这样的:

routerInstance.navigate("game", {trigger: true});

http://backbonejs.org/#Router-navigate