Jitu
Jitu

Reputation: 284

How to handle back using Backbone.js? Or how to render the earlier visited page without making call to fetch again?

I am creating a web app using backbone.js in which there are multiple views which are rendering fine. The main problem is handling the back button event. When i press a back button the earler view renders properly but by following the same process of calling the fetch(). I dont want the call the fetch() instead render the view with the earlier data received.

Is there any way to do it?

Upvotes: 0

Views: 317

Answers (1)

Tanzeeb Khalili
Tanzeeb Khalili

Reputation: 7344

You essentially want to have a condition on the fetch. There are a couple of approaches, one would be to assign it to the instance like this:

myAction: function() {
    if (!this.collection) {
        this.collection = new MyCollection();
        this.collection.fetch()
    }
    view = new View({collection: this.collection});
    // ... render ...
}

Upvotes: 3

Related Questions