A Backbone plugin to cache calls to Backbone.Model.prototype.fetch
and
Backbone.Model.prototype.fetch
in memory and localStorage
.
This plugin intercepts calls to fetch
and stores the results in a cache object (Backbone.fetchCache._cache
). If fetch is called with { cache: true }
in the options and the URL has already been cached the AJAX call will be skipped.
The local cache is persisted in localStorage
if available for faster initial page loads.
The prefill
option allows for models and collections to be filled with cache data just until the fetch
operations are complete -- a nice way to make the app feel snappy on slower connections.
Nothing. This plugin is primarily for working with an API where you don't have control over response cache headers.
Add the script to the page after backbone.js has been included:
<script src="/path/to/backbone.js"></script>
<script src="/path/to/backbone.fetch-cache.js"></script>
Calls to modelInstance.fetch
or collectionInstance.fetch
will be fulfilled from the cache (if possible) when cache: true
is set in the options hash:
myModel.fetch({ cache: true });
myCollection.fetch({ cache: true });
This option allows the model/collection to be populated from the cache immediately and then be updated once the call to fetch
has completed. The initial cache hit calls the prefillSuccess
callback and then the AJAX success/error callbacks are called as normal when the request is complete. This allows the page to render something immediately and then update it after the request completes. (Note: the prefillSuccess
callback will not fire if the data is not found in the cache.)
myModel.fetch({
prefill: true,
prefillSuccess: someCallback, // Fires when the cache hit happens
success: anotherCallback // Fires after the AJAX call
});
myCollection.fetch({
prefill: true,
prefillSuccess: someCallback, // Fires when the cache hit happens
success: anotherCallback // Fires after the AJAX call
});
This option can be used with the promises interface like so (note: the progress
event will not fire if the data is not found in the cache.):
var modelPromise = myModel.fetch({ prefill: true });
modelPromise.progress(someCallback); // Fires when the cache hit happens
modelPromise.done(anotherCallback); // Fires after the AJAX call
var collectionPromise = myModel.fetch({ prefill: true });
collectionPromise.progress(someCallback); // Fires when the cache hit happens
collectionPromise.done(anotherCallback); // Fires after the AJAX call
Cache vales expire after 5 minutes by default. You can adjust this by passing
expires: <seconds>
to the fetch call. Set to false
to never expire:
myModel.fetch({ cache: true, expires: 60000 });
myCollection.fetch({ cache: true, expires: 60000 });
// These will never expire
myModel.fetch({ cache: true, expires: false });
myCollection.fetch({ cache: expires: false });
By default the cache is persisted in localStorage (if available). Set Backbone.fetchCache.localStorage = false
to disable this:
Backbone.fetchCache.localStorage = false;
You can run the tests by cloning the repo, installing the dependencies and
running grunt jasmine
:
$ npm install
$ grunt jasmine
To see the tests in a browser, run:
$ grunt jasmine-server
The default grunt task runs tests and lints the code.
$ grunt
- v0.1.1: Add
prefetch
option.