angular-cached-resource
Version:
An AngularJS module to interact with RESTful resources, even when browser is offline
53 lines (45 loc) • 2.24 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
module.exports = function(providerParams) {
var $log, ResourceCacheArrayEntry, ResourceCacheEntry;
$log = providerParams.$log;
ResourceCacheEntry = require('./resource_cache_entry')(providerParams);
return ResourceCacheArrayEntry = (function(superClass) {
extend(ResourceCacheArrayEntry, superClass);
function ResourceCacheArrayEntry() {
return ResourceCacheArrayEntry.__super__.constructor.apply(this, arguments);
}
ResourceCacheArrayEntry.prototype.defaultValue = [];
ResourceCacheArrayEntry.prototype.cacheKeyPrefix = function() {
return this.key + "/array";
};
ResourceCacheArrayEntry.prototype.addInstances = function(instances, dirty, options) {
var cacheArrayReferences, cacheInstanceEntry, cacheInstanceParams, i, instance, len;
if (options == null) {
options = {
append: false
};
}
cacheArrayReferences = options.append ? this.value : [];
if (cacheArrayReferences == null) {
cacheArrayReferences = [];
}
for (i = 0, len = instances.length; i < len; i++) {
instance = instances[i];
cacheInstanceParams = instance.$params();
if (Object.keys(cacheInstanceParams).length === 0) {
$log.error("'" + this.key + "' instance doesn't have any boundParams. Please, make sure you specified them in your resource's initialization, f.e. `{id: \"@id\"}`, or it won't be cached.");
} else {
cacheArrayReferences.push(cacheInstanceParams);
cacheInstanceEntry = new ResourceCacheEntry(this.key, cacheInstanceParams).load();
if (!(options.append && (cacheInstanceEntry.value != null))) {
cacheInstanceEntry.set(instance, dirty);
}
}
}
return this.set(cacheArrayReferences, dirty);
};
return ResourceCacheArrayEntry;
})(ResourceCacheEntry);
};