public
Last active

Backbone.Collection caching by URL

  • Download Gist
backbone.collectioncache.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*!
* backbone.collectioncache.js v0.0.2
* Copyright 2012, Tim Branyen (@tbranyen)
* backbone.collectioncache.js may be freely distributed under the MIT license.
*/
(function(window) {
 
"use strict";
 
// Dependencies
var Backbone = window.Backbone;
var _ = window._;
var $ = window.$;
// If session doesn't exist, simply use a plain object.
var sessionStorage = window.sessionStorage || {};
 
// Maintain an in-memory cache.
function Cache() {}
// Set the prototype to sessionStorage.
Cache.prototype = sessionStorage;
 
// Create a new Cache.
var cache = new Cache();
 
// Override sync on Collections, allowing them to cache.
Backbone.Collection.prototype.sync = function(method, collection, options) {
// Get the correct URL.
var url = _.isFunction(collection.url) ? collection.url() : collection.url;
 
// Check for cache property and if an existing cache exists.
if (collection.cache === true && cache[url]) {
// Extract from sessionStroage and place into memory.
if (_.isString(cache[url])) {
cache[url] = JSON.parse(cache[url]);
}
 
// Trigger the success with the correct data.
options.success.apply(this, cache[url]);
 
// Emulate the jqXHR.
return $.Deferred().resolve();
}
 
// Call out to default implementation.
var jqXHR = Backbone.sync.apply(this, arguments);
// Wait until complete and if successful, cache!
jqXHR.then(function() {
cache[url] = _.toArray(arguments);
sessionStorage[url] = JSON.stringify([arguments[0], "success", {}]);
});
 
// Emulate normal Sync.
return jqXHR;
};
 
})(this);

Nice to have a feature to invalidate cache after edit or create cached entity.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.