How to handle back using Backbone.js? Or how to render the earlier visited page without making call to fetch again?
-
13-12-2019 - |
Question
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?
Solution
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 ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow