From 282dd23d1b578d058f8ee4f2a2eda5382805becb Mon Sep 17 00:00:00 2001 From: Roman Kozlovskyi Date: Thu, 12 Sep 2013 22:05:12 +0300 Subject: [PATCH] backbone.fetch-cache v1.1.1 --- .../1.1.1/backbone.fetch-cache.js | 941 ++++++++++++++++++ .../1.1.1/backbone.fetch-cache.min.js | 613 ++++++++++++ ajax/libs/backbone.fetch-cache/package.json | 2 +- 3 files changed, 1555 insertions(+), 1 deletion(-) create mode 100644 ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.js create mode 100644 ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.min.js diff --git a/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.js b/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.js new file mode 100644 index 000000000..5b711e1b6 --- /dev/null +++ b/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.js @@ -0,0 +1,941 @@ + + + + + + + + + backbone-fetch-cache/backbone.fetch-cache.js at v1.1.1 · mrappleton/backbone-fetch-cache · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+
+ + + + + + + +
+ + +
+ + + + + +
+ + This repository + + +
+
+ +
+ + +
This repository
+
+ +
+ + +
All repositories
+
+ +
+
+
+ + + + + + + + +
+
+ +
+
+ + + + + +
+ +
+
+ + + + +

+ public + + + /backbone-fetch-cache + + + Octocat-spinner-32 + + +

+
+
+ +
+ +
+ +
+ + + + +
+ + + + +
+

HTTPS clone URL

+
+ + + +
+
+ + + +
+

Subversion checkout URL

+
+ + + +
+
+ + +

You can clone with + HTTPS, + or Subversion. + + + + + +

+ + + + + + Download ZIP + +
+
+ +
+ + + + + + + + +Show File Finder + +
+ + + +
+ + + tag: + v1.1.1 + + +
+ +
+
+ Switch branches/tags + +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + +
+ + master +
+
+ +
Nothing to show
+
+ +
+
+ + +
+ + v1.1.1 +
+
+ + v1.1.0 +
+
+ + v1.0.2 +
+
+ + v1.0.1 +
+
+ + v1.0.0 +
+
+ + v0.1.11 +
+
+ + v0.1.10 +
+
+ + v0.1.9 +
+
+ + v0.1.8 +
+
+ + v0.1.7 +
+
+ + v0.1.6 +
+
+ + v0.1.5 +
+
+ + v0.1.4 +
+
+ + v0.1.3 +
+
+ + 1.0.2 +
+
+ +
Nothing to show
+
+ +
+
+
+ + +
+ + + +
+ + + +
+ v1.1.1 +
+ +
+

9 contributors

+ + + + + + + + + + + +
+ +
+ + +
+
+
+
+ + file + 303 lines (248 sloc) + 9.468 kb +
+ + +
+
+ + + + + +
+ 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 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281 +282 +283 +284 +285 +286 +287 +288 +289 +290 +291 +292 +293 +294 +295 +296 +297 +298 +299 +300 +301 +302 + + +
/*!
backbone.fetch-cache v1.1.1
by Andy Appleton - https://github.com/mrappleton/backbone-fetch-cache.git
*/

// AMD wrapper from https://github.com/umdjs/umd/blob/master/amdWebGlobal.js

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD. Register as an anonymous module and set browser global
    define(['underscore', 'backbone', 'jquery'], function (_, Backbone, $) {
      return (root.Backbone = factory(_, Backbone, $));
    });
  } else {
    // Browser globals
    root.Backbone = factory(root._, root.Backbone, root.jQuery);
  }
}(this, function (_, Backbone, $) {

  // Setup
  var superMethods = {
    modelFetch: Backbone.Model.prototype.fetch,
    modelSync: Backbone.Model.prototype.sync,
    collectionFetch: Backbone.Collection.prototype.fetch
  },
  supportLocalStorage = (function() {
    var supported = typeof window.localStorage !== 'undefined';
    if (supported) {
      try {
        // impossible to write on some platforms when private browsing is on and
        // throws an exception = local storage not supported.
        localStorage.setItem("test_support", "test_support");
        localStorage.removeItem("test_support");
      } catch (e) {
        supported = false;
      }
    }
    return supported;
  })();

  Backbone.fetchCache = (Backbone.fetchCache || {});
  Backbone.fetchCache._cache = (Backbone.fetchCache._cache || {});

  Backbone.fetchCache.priorityFn = function(a, b) {
    if (!a || !a.expires || !b || !b.expires) {
      return a;
    }

    return a.expires - b.expires;
  };

  Backbone.fetchCache._prioritize = function() {
    var sorted = _.values(this._cache).sort(this.priorityFn);
    var index = _.indexOf(_.values(this._cache), sorted[0]);
    return _.keys(this._cache)[index];
  };

  Backbone.fetchCache._deleteCacheWithPriority = function() {
    Backbone.fetchCache._cache[this._prioritize()] = null;
    delete Backbone.fetchCache._cache[this._prioritize()];
    Backbone.fetchCache.setLocalStorage();
  };

  if (typeof Backbone.fetchCache.localStorage === 'undefined') {
    Backbone.fetchCache.localStorage = true;
  }

  // Shared methods
  function getCacheKey(instance, opts) {
    var url;

    if(opts && opts.url) {
      url = opts.url;
    } else {
      url = _.isFunction(instance.url) ? instance.url() : instance.url;
    }

    // Need url to use as cache key so return if we can't get it
    if(!url) { return; }

    if(opts && opts.data) {
      return url + "?" + $.param(opts.data);
    }
    return url;
  }

  function setCache(instance, opts, attrs) {
    opts = (opts || {});
    var key = Backbone.fetchCache.getCacheKey(instance, opts),
        expires = false;

    // Need url to use as cache key so return if we can't get it
    if (!key) { return; }

    // Never set the cache if user has explicitly said not to
    if (opts.cache === false) { return; }

    // Don't set the cache unless cache: true or prefill: true option is passed
    if (!(opts.cache || opts.prefill)) { return; }

    if (opts.expires !== false) {
      expires = (new Date()).getTime() + ((opts.expires || 5 * 60) * 1000);
    }

    Backbone.fetchCache._cache[key] = {
      expires: expires,
      value: attrs
    };

    Backbone.fetchCache.setLocalStorage();
  }

  function clearItem(key) {
    delete Backbone.fetchCache._cache[key];
    Backbone.fetchCache.setLocalStorage();
  }

  function setLocalStorage() {
    if (!supportLocalStorage || !Backbone.fetchCache.localStorage) { return; }
    try {
      localStorage.setItem('backboneCache', JSON.stringify(Backbone.fetchCache._cache));
    } catch (err) {
      var code = err.code || err.number || err.message;
      if (code === 22) {
        this._deleteCacheWithPriority();
      } else {
        throw(err);
      }
    }
  }

  function getLocalStorage() {
    if (!supportLocalStorage || !Backbone.fetchCache.localStorage) { return; }
    var json = localStorage.getItem('backboneCache') || '{}';
    Backbone.fetchCache._cache = JSON.parse(json);
  }

  function nextTick(fn) {
    return window.setTimeout(fn, 0);
  }

  // Instance methods
  Backbone.Model.prototype.fetch = function(opts) {
    opts = (opts || {});
    var key = Backbone.fetchCache.getCacheKey(this, opts),
        data = Backbone.fetchCache._cache[key],
        expired = false,
        attributes = false,
        deferred = new $.Deferred(),
        self = this;

    function setData() {
      self.set(self.parse(attributes), opts);
      if (_.isFunction(opts.prefillSuccess)) { opts.prefillSuccess(self, attributes, opts); }

      // Trigger sync events
      self.trigger('cachesync', self, attributes, opts);
      self.trigger('sync', self, attributes, opts);

      // Notify progress if we're still waiting for an AJAX call to happen...
      if (opts.prefill) { deferred.notify(self); }
      // ...finish and return if we're not
      else {
        if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); }
        deferred.resolve(self);
      }
    }

    if (data) {
      expired = data.expires;
      expired = expired && data.expires < (new Date()).getTime();
      attributes = data.value;
    }

    if (!expired && (opts.cache || opts.prefill) && attributes) {
      // Ensure that cache resolution adhers to async option, defaults to true.
      if (opts.async == null) { opts.async = true; }

      if (opts.async) {
        nextTick(setData);
      } else {
        setData();
      }

      if (!opts.prefill) {
        return deferred.promise();
      }
    }

    // Delegate to the actual fetch method and store the attributes in the cache
    superMethods.modelFetch.apply(this, arguments)
      // resolve the returned promise when the AJAX call completes
      .done( _.bind(deferred.resolve, this, this) )
      // Set the new data in the cache
      .done( _.bind(Backbone.fetchCache.setCache, null, this, opts) )
      // Reject the promise on fail
      .fail( _.bind(deferred.reject, this, this) );

    // return a promise which provides the same methods as a jqXHR object
    return deferred.promise();
  };

  // Override Model.prototype.sync and try to clear cache items if it looks
  // like they are being updated.
  Backbone.Model.prototype.sync = function(method, model, options) {
    // Only empty the cache if we're doing a create, update, patch or delete.
    if (method === 'read') {
      return superMethods.modelSync.apply(this, arguments);
    }

    var collection = model.collection,
        keys = [],
        i, len;

    // Build up a list of keys to delete from the cache, starting with this
    keys.push(Backbone.fetchCache.getCacheKey(model));

    // If this model has a collection, also try to delete the cache for that
    if (!!collection) {
      keys.push(Backbone.fetchCache.getCacheKey(collection));
    }

    // Empty cache for all found keys
    for (i = 0, len = keys.length; i < len; i++) { clearItem(keys[i]); }

    return superMethods.modelSync.apply(this, arguments);
  };

  Backbone.Collection.prototype.fetch = function(opts) {
    opts = (opts || {});
    var key = Backbone.fetchCache.getCacheKey(this, opts),
        data = Backbone.fetchCache._cache[key],
        expired = false,
        attributes = false,
        deferred = new $.Deferred(),
        self = this;

    function setData() {
      self[opts.reset ? 'reset' : 'set'](self.parse(attributes), opts);
      if (_.isFunction(opts.prefillSuccess)) { opts.prefillSuccess(self); }

      // Trigger sync events
      self.trigger('cachesync', self, attributes, opts);
      self.trigger('sync', self, attributes, opts);

      // Notify progress if we're still waiting for an AJAX call to happen...
      if (opts.prefill) { deferred.notify(self); }
      // ...finish and return if we're not
      else {
        if (_.isFunction(opts.success)) { opts.success(self, attributes, opts); }
        deferred.resolve(self);
      }
    }

    if (data) {
      expired = data.expires;
      expired = expired && data.expires < (new Date()).getTime();
      attributes = data.value;
    }

    if (!expired && (opts.cache || opts.prefill) && attributes) {
      // Ensure that cache resolution adhers to async option, defaults to true.
      if (opts.async == null) { opts.async = true; }

      if (opts.async) {
        nextTick(setData);
      } else {
        setData();
      }

      if (!opts.prefill) {
        return deferred.promise();
      }
    }

    // Delegate to the actual fetch method and store the attributes in the cache
    superMethods.collectionFetch.apply(this, arguments)
      // resolve the returned promise when the AJAX call completes
      .done( _.bind(deferred.resolve, this, this) )
      // Set the new data in the cache
      .done( _.bind(Backbone.fetchCache.setCache, null, this, opts) )
      // Reject the promise on fail
      .fail( _.bind(deferred.reject, this, this) );

    // return a promise which provides the same methods as a jqXHR object
    return deferred.promise();
  };

  // Prime the cache from localStorage on initialization
  getLocalStorage();

  // Exports

  Backbone.fetchCache._superMethods = superMethods;
  Backbone.fetchCache.setCache = setCache;
  Backbone.fetchCache.getCacheKey = getCacheKey;
  Backbone.fetchCache.clearItem = clearItem;
  Backbone.fetchCache.setLocalStorage = setLocalStorage;
  Backbone.fetchCache.getLocalStorage = getLocalStorage;

  return Backbone;
}));
+
+
+ +
+
+ + + + +
+ +
+ +
+
+ + +
+ +
+ +
+ + +
+
+
+ +
+
+
+
+
+
+ +
+ + + +
+ + + Something went wrong with that request. Please try again. +
+ + + + + diff --git a/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.min.js b/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.min.js new file mode 100644 index 000000000..be1d2855e --- /dev/null +++ b/ajax/libs/backbone.fetch-cache/1.1.1/backbone.fetch-cache.min.js @@ -0,0 +1,613 @@ + + + + + + + + + backbone-fetch-cache/backbone.fetch-cache.min.js at v1.1.1 · mrappleton/backbone-fetch-cache · GitHub + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+
+ + + + + + + +
+ + +
+ + + + + +
+ + This repository + + +
+
+ +
+ + +
This repository
+
+ +
+ + +
All repositories
+
+ +
+
+
+ + + + + + + + +
+
+ +
+
+ + + + + +
+ +
+
+ + + + +

+ public + + + /backbone-fetch-cache + + + Octocat-spinner-32 + + +

+
+
+ +
+ +
+ +
+ + + + +
+ + + + +
+

HTTPS clone URL

+
+ + + +
+
+ + + +
+

Subversion checkout URL

+
+ + + +
+
+ + +

You can clone with + HTTPS, + or Subversion. + + + + + +

+ + + + + + Download ZIP + +
+
+ +
+ + + + + + + + +Show File Finder + +
+ + + +
+ + + tag: + v1.1.1 + + +
+ +
+
+ Switch branches/tags + +
+ +
+
+ +
+
+ +
+
+ +
+ +
+ + +
+ + master +
+
+ +
Nothing to show
+
+ +
+
+ + +
+ + v1.1.1 +
+
+ + v1.1.0 +
+
+ + v1.0.2 +
+
+ + v1.0.1 +
+
+ + v1.0.0 +
+
+ + v0.1.11 +
+
+ + v0.1.10 +
+
+ + v0.1.9 +
+
+ + v0.1.8 +
+
+ + v0.1.7 +
+
+ + v0.1.6 +
+
+ + v0.1.5 +
+
+ + v0.1.4 +
+
+ + v0.1.3 +
+
+ + 1.0.2 +
+
+ +
Nothing to show
+
+ +
+
+
+ + +
+ + + +
+ + + +
+ v1.1.1 +
+ +
+

3 contributors

+ + + + + +
+ +
+ + +
+
+
+
+ + file + 4 lines (4 sloc) + 4.207 kb +
+ + +
+
+ + + + + +
+ 1 +2 +3 +4 + + +
/* backbone.fetch-cache v1.1.1 (2013-09-10)
by Andy Appleton - https://github.com/mrappleton/backbone-fetch-cache.git
*/
(function(e,c){"function"==typeof define&&define.amd?define(["underscore","backbone","jquery"],function(_,Backbone,t){return e.Backbone=c(_,Backbone,t)}):e.Backbone=c(e._,e.Backbone,e.jQuery)})(this,function(_,Backbone,e){function c(c,t){var a;return(a=t&&t.url?t.url:_.isFunction(c.url)?c.url():c.url)?t&&t.data?a+"?"+e.param(t.data):a:void 0}function t(e,c,t){c=c||{};var a=Backbone.fetchCache.getCacheKey(e,c),h=!1;a&&c.cache!==!1&&(c.cache||c.prefill)&&(c.expires!==!1&&(h=(new Date).getTime()+1e3*(c.expires||300)),Backbone.fetchCache._cache[a]={expires:h,value:t},Backbone.fetchCache.setLocalStorage())}function a(e){delete Backbone.fetchCache._cache[e],Backbone.fetchCache.setLocalStorage()}function h(){if(n&&Backbone.fetchCache.localStorage)try{localStorage.setItem("backboneCache",JSON.stringify(Backbone.fetchCache._cache))}catch(e){var c=e.code||e.number||e.message;if(22!==c)throw e;this._deleteCacheWithPriority()}}function r(){if(n&&Backbone.fetchCache.localStorage){var e=localStorage.getItem("backboneCache")||"{}";Backbone.fetchCache._cache=JSON.parse(e)}}function i(e){return window.setTimeout(e,0)}var o={modelFetch:Backbone.Model.prototype.fetch,modelSync:Backbone.Model.prototype.sync,collectionFetch:Backbone.Collection.prototype.fetch},n=function(){var e=window.localStorage!==void 0;if(e)try{localStorage.setItem("test_support","test_support"),localStorage.removeItem("test_support")}catch(c){e=!1}return e}();return Backbone.fetchCache=Backbone.fetchCache||{},Backbone.fetchCache._cache=Backbone.fetchCache._cache||{},Backbone.fetchCache.priorityFn=function(e,c){return e&&e.expires&&c&&c.expires?e.expires-c.expires:e},Backbone.fetchCache._prioritize=function(){var e=_.values(this._cache).sort(this.priorityFn),c=_.indexOf(_.values(this._cache),e[0]);return _.keys(this._cache)[c]},Backbone.fetchCache._deleteCacheWithPriority=function(){Backbone.fetchCache._cache[this._prioritize()]=null,delete Backbone.fetchCache._cache[this._prioritize()],Backbone.fetchCache.setLocalStorage()},Backbone.fetchCache.localStorage===void 0&&(Backbone.fetchCache.localStorage=!0),Backbone.Model.prototype.fetch=function(c){function t(){l.set(l.parse(n),c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l,n,c),l.trigger("cachesync",l,n,c),l.trigger("sync",l,n,c),c.prefill?s.notify(l):(_.isFunction(c.success)&&c.success(l,n,c),s.resolve(l))}c=c||{};var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,n=!1,s=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),n=h.value),r||!c.cache&&!c.prefill||!n||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.modelFetch.apply(this,arguments).done(_.bind(s.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(s.reject,this,this)),s.promise()):s.promise()},Backbone.Model.prototype.sync=function(e,c){if("read"===e)return o.modelSync.apply(this,arguments);var t,h,r=c.collection,i=[];for(i.push(Backbone.fetchCache.getCacheKey(c)),r&&i.push(Backbone.fetchCache.getCacheKey(r)),t=0,h=i.length;h>t;t++)a(i[t]);return o.modelSync.apply(this,arguments)},Backbone.Collection.prototype.fetch=function(c){function t(){l[c.reset?"reset":"set"](l.parse(n),c),_.isFunction(c.prefillSuccess)&&c.prefillSuccess(l),l.trigger("cachesync",l,n,c),l.trigger("sync",l,n,c),c.prefill?s.notify(l):(_.isFunction(c.success)&&c.success(l,n,c),s.resolve(l))}c=c||{};var a=Backbone.fetchCache.getCacheKey(this,c),h=Backbone.fetchCache._cache[a],r=!1,n=!1,s=new e.Deferred,l=this;return h&&(r=h.expires,r=r&&h.expires<(new Date).getTime(),n=h.value),r||!c.cache&&!c.prefill||!n||(null==c.async&&(c.async=!0),c.async?i(t):t(),c.prefill)?(o.collectionFetch.apply(this,arguments).done(_.bind(s.resolve,this,this)).done(_.bind(Backbone.fetchCache.setCache,null,this,c)).fail(_.bind(s.reject,this,this)),s.promise()):s.promise()},r(),Backbone.fetchCache._superMethods=o,Backbone.fetchCache.setCache=t,Backbone.fetchCache.getCacheKey=c,Backbone.fetchCache.clearItem=a,Backbone.fetchCache.setLocalStorage=h,Backbone.fetchCache.getLocalStorage=r,Backbone});
+
+
+ +
+
+ + + + +
+ +
+ +
+
+ + +
+ +
+ +
+ + +
+
+
+ +
+
+
+
+
+
+ +
+ + + +
+ + + Something went wrong with that request. Please try again. +
+ + + + + diff --git a/ajax/libs/backbone.fetch-cache/package.json b/ajax/libs/backbone.fetch-cache/package.json index feb7df177..b252946ee 100644 --- a/ajax/libs/backbone.fetch-cache/package.json +++ b/ajax/libs/backbone.fetch-cache/package.json @@ -1,7 +1,7 @@ { "name": "backbone.fetch-cache", "filename": "backbone.fetch-cache.min.js", - "version": "1.1.0", + "version": "1.1.1", "author": "Andy Appleton", "description": "Caches calls to Backbone.[Model | Collection].fetch", "homepage": "https://github.com/mrappleton/backbone-fetch-cache",