antonjs
antonjs

Reputation: 14318

How can I cache the result of a fetch call using requireJs?

I have several views which rendering result depends on the same collection and then other models or collections:

Examaples:

View1 -> model1, commonCollection
View2 -> collection2, commonCollection
View3 -> model3, commonCollection

Right now, to solve the problem I make commonCollection.fetch for each view… and before to render the page I wait that each model/collection is ready for each view as explained in this answer.
But since the result of commonCollection is the same, I would like to cache the result for using it for other views.

How can I cache the result of commonCollection without using global variables? I am using RequireJs

Upvotes: 1

Views: 84

Answers (1)

Matt Wonlaw
Matt Wonlaw

Reputation: 12443

can't you just override fetch in common collection to do the caching?

Backbone.Collection.extend({
   ...
   fetch: function(options) {
      if (alreadyUpdated) {
         // do nothing
      } else {
          return Backbone.Collection.prototype.fetch.apply(this, arguments);
      }
   }
   ...
});

Here is a fiddle of a more complete example: http://jsfiddle.net/4A8Wu/2/

Upvotes: 2

Related Questions