我用同樣的答案從jevakallio,但我有這樣的評論者傑伊·庫馬爾有同樣的問題:routesHit
不減去這樣打appRouter.back()
足夠的時間將用戶退出應用程序的,所以我加了3條線:
var AppRouter = Backbone.Router.extend({
initialize: function() {
this.routesHit = 0;
//keep count of number of routes handled by your application
Backbone.history.on('route', function() { this.routesHit++; }, this);
},
back: function() {
if(this.routesHit > 1) {
//more than one route hit -> user did not land to current page directly
this.routesHit = this.routesHit - 2; //Added line: read below
window.history.back();
} else {
//otherwise go to the home page. Use replaceState if available so
//the navigation doesn't create an extra history entry
if(Backbone.history.getFragment() != 'app/') //Added line: read below
this.routesHit = 0; //Added line: read below
this.navigate('app/', {trigger:true, replace:true});
}
}
});
,並使用RO uter方法導航回:
appRouter.back();
新增線路:
一日一:從routesHit
減去2,那麼當其重定向到「返回」頁面,它會獲得1點所以它實際上是像你這樣剛減1。
第二個:如果用戶已經在「家」,不會是一個重定向,所以不要做任何事情routesHit
。
第三個:如果用戶是他在哪裏開始並且被髮送回「家」,設置routesHit = 0
,那麼當重定向到「家」時routesHit
將再次爲1。
這種方法統計包括瀏覽器後退導航在內的所有路線。假設我在我的應用程序內導航到3條路線,那麼routesHit將爲3.現在使用瀏覽器後退按鈕不會減少routesHit(而是增加它),瀏覽器最終會將您從應用程序中移出。 – 2013-09-18 08:40:08