UNPKG

angular-cached-resource

Version:

An AngularJS module to interact with RESTful resources, even when browser is offline

67 lines (57 loc) 1.97 kB
// Generated by CoffeeScript 1.7.1 module.exports = function(providerParams) { var $log, Cache, ResourceCacheEntry; $log = providerParams.$log; Cache = require('./cache')(providerParams); return ResourceCacheEntry = (function() { ResourceCacheEntry.prototype.defaultValue = {}; ResourceCacheEntry.prototype.cacheKeyPrefix = function() { return this.key; }; ResourceCacheEntry.prototype.fullCacheKey = function() { return this.cacheKeyPrefix() + this.cacheKeyParams; }; function ResourceCacheEntry(key, params) { var param, paramKeys; this.key = key; paramKeys = angular.isObject(params) ? Object.keys(params).sort() : []; if (paramKeys.length) { this.cacheKeyParams = '?' + ((function() { var _i, _len, _results; _results = []; for (_i = 0, _len = paramKeys.length; _i < _len; _i++) { param = paramKeys[_i]; _results.push("" + param + "=" + params[param]); } return _results; })()).join('&'); } else { this.cacheKeyParams = ''; } } ResourceCacheEntry.prototype.load = function() { var _ref; _ref = Cache.getItem(this.fullCacheKey(), this.defaultValue), this.value = _ref.value, this.dirty = _ref.dirty; return this; }; ResourceCacheEntry.prototype.set = function(value, dirty) { this.value = value; if (this.dirty && !dirty) { $log.error("unexpectedly setting a clean entry (load) over a dirty entry (pending write)"); } this.dirty = dirty; return this._update(); }; ResourceCacheEntry.prototype.setClean = function() { this.dirty = false; return this._update(); }; ResourceCacheEntry.prototype._update = function() { return Cache.setItem(this.fullCacheKey(), { value: this.value, dirty: this.dirty }); }; return ResourceCacheEntry; })(); };