backbone.history.naviguate触发路由器停留在同一页面上

时间:2015-04-27 18:19:15

标签: backbone.js

我的问题很简单。

我启动/ app,当我点击某个按钮时,我会触发/报告     Backbone.history.navigate(' report',{trigger:true,replace:true});

然后我在/报告页面。没问题。

我在/ report上有相同的按钮,我想触发路由器路由"报告"当我点击它。它似乎无法正常工作,因为我已经在被询问的页面上。

知道我该怎么办?

非常感谢

1 个答案:

答案 0 :(得分:1)

Backbone.history.navigate('report',{trigger: true, replace: true});

您不应该使用它来呈现/重新加载您的应用程序视图。不建议这样做。

相反,你应该有一个扩展Backbone.Events

的控制器对象(一个js对象)

为路线定义回调" / report"如下:

var MyAppBus = _.extend({},Backbone.Events);
MyAppBus.on("report",function(payLoad){
    // 1.Load the data
    // 2.Create an instance of the view passing the loaded data.
    // (In your case, you could also pass an instance of the view in the payload 
    // and update the data alone in the view instance.)
    // 3.Render the view.
    // 4.Call the router navigate function with trigger set to false to  
    //   update the url on a need basis. 
});

现在,在event/router callback你可以这样做:

MyAppBus.trigger("report",payLoad);

事件回调中的payLoad可以是:

var payLoad = {};
payLoad.view = viewInstance; 
相关问题