1

I'm developing a Backbone application with multiple views, collections, etc.. It also has a router that handles permanent URL pretty well.

What I'm trying to achieve is creating a "Back" button which allows user to go back window.history(-1) to a previous view when it is available (user came from that view earlier). And if not, the "back" button would lead to the home page.

Basically I need to check whether or not Backbone has saved something in its history I can use to roll back one step. If the history is empty (we navigated to this URL directly) back point to the main router.

I already tried using window.hisory.length but it is never empty. It may still point to the starting homepage of the browser or browser's blank opening page.

Does Backbone.History keeps track of the URLs (hashes) that passes through it? If so - how to I access it. I already tried console logging Backbone.History but found nothing I can hold on to.

Any help / advise is appreciated.

4

1 回答 1

0

您可以执行以下操作:

路由器初始化功能 -

this.on("all", this.storeRoute);
this.history = [];

然后是 storeRoute 功能(也在路由器中) -

storeRoute: function() {
    return this.history.push(Backbone.history.fragment);
}

也在路由器 -

previous: function(){
    if (this.history.length > 1) {
      return this.navigate(this.history[this.history.length - 2], true);
    }
}

所以,你可以随时打电话yourApp.router.previous()

于 2013-10-23T08:39:53.077 回答