Reputation: 9644
I think I might start making some frequent overlapping "fetch" calls in Backbone. What would be the best way to cache these calls and re-use them?
Upvotes: 2
Views: 571
Reputation: 1194
There are a lot of possibilities to achieve this. One would be to implement you own version of the Backbone.sync method. There you can then cache your results (eg. as url - result pairs). When fetch is called you lookup the url and either return the cached result or fetch the result from the server.
A more sophisticated approach would be to use a sort of proxy collection. This is a globally available collection and the only one that can make serverside fetches. All other collection get a custom fetch method implementation fetching their models via the proxy. The proxy caches serverside result and also creates the model instances (and keeps them). So you stay in controll of model instances and prevent you from having multiple models with the same id in your application.
Upvotes: 2