angular-cached-resource
Version:
An AngularJS module to interact with RESTful resources, even when browser is offline
59 lines (50 loc) • 1.69 kB
JavaScript
// Generated by CoffeeScript 1.10.0
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;
this.dirty = dirty;
return this._update();
};
ResourceCacheEntry.prototype._update = function() {
return Cache.setItem(this.fullCacheKey(), {
value: this.value,
dirty: this.dirty
});
};
return ResourceCacheEntry;
})();
};