2015-04-27 23 views
0

我的問題很簡單。backbone.history.naviguate觸發路由器停留在同一頁面

我開始在/ app上,當我點擊某個按鈕時,我觸發/報告了 Backbone.history.navigate('report',{trigger:true,replace:true});

然後我在/報告頁面。沒問題。

我在/報告上有相同的按鈕,我想點擊它時觸發路由器路由「報告」。這看起來不起作用,因爲我已經在提問頁面上了。

任何想法我應該怎麼做?

非常感謝

+0

有一個簡單的檢查 - 「if(this.fragment === fragment)return;'。也許你需要開展一些活動並在另一個地方抓住它,然後只是做你想做的事情? –

+0

我會在你點擊的按鈕上重新渲染頁面的按鈕。就像Vahan所說的那樣,Backbone在散列片段相同時不會觸發事件。 –

+0

如果您必須...您可以做到這一點 window.onhashchange = function(){ }; –

回答

1

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

你不應該使用這種渲染/不建議重裝應用程序views.This。

相反,你應該有一個控制器對象(JS對象)延伸Backbone.Events

定義一個回調的的路線「/報告」如下:

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); 

當有效載荷在事件回調可能是:

var payLoad = {}; 
payLoad.view = viewInstance; 
相關問題