angular-cached-resource
Version:
An AngularJS module to interact with RESTful resources, even when browser is offline
221 lines (204 loc) • 7.55 kB
JavaScript
// Generated by CoffeeScript 1.10.0
var DEFAULT_ACTIONS, buildCachedResourceClass, readArrayCache, readCache, writeCache;
DEFAULT_ACTIONS = {
get: {
method: 'GET'
},
query: {
method: 'GET',
isArray: true
},
save: {
method: 'POST'
},
remove: {
method: 'DELETE'
},
"delete": {
method: 'DELETE'
}
};
readArrayCache = require('./read_array_cache');
readCache = require('./read_cache');
writeCache = require('./write_cache');
module.exports = buildCachedResourceClass = function($resource, $timeout, $q, providerParams, args) {
var $key, $log, Cache, CachedResource, Resource, ResourceCacheArrayEntry, ResourceCacheEntry, ResourceWriteQueue, actionConfig, actionName, actions, arg, boundParams, handler, isPermissibleBoundValue, method, param, paramDefault, paramDefaults, url;
$log = providerParams.$log;
ResourceCacheEntry = require('./resource_cache_entry')(providerParams);
ResourceCacheArrayEntry = require('./resource_cache_array_entry')(providerParams);
ResourceWriteQueue = require('./resource_write_queue')(providerParams, $q);
Cache = require('./cache')(providerParams);
$key = args.shift();
url = args.shift();
while (args.length) {
arg = args.pop();
if (angular.isObject(arg[Object.keys(arg)[0]])) {
actions = arg;
} else {
paramDefaults = arg;
}
}
actions = angular.extend({}, DEFAULT_ACTIONS, actions);
if (paramDefaults == null) {
paramDefaults = {};
}
boundParams = {};
for (param in paramDefaults) {
paramDefault = paramDefaults[param];
if (paramDefault[0] === '@') {
boundParams[paramDefault.substr(1)] = param;
}
}
Resource = $resource.call(null, url, paramDefaults, actions);
isPermissibleBoundValue = function(value) {
return angular.isDate(value) || angular.isNumber(value) || angular.isString(value);
};
CachedResource = (function() {
CachedResource.prototype.$cache = true;
function CachedResource(attrs) {
angular.extend(this, attrs);
}
CachedResource.prototype.toJSON = function() {
var data;
data = angular.extend({}, this);
delete data.$promise;
delete data.$httpPromise;
return data;
};
CachedResource.prototype.$params = function() {
var attribute, params;
params = {};
for (attribute in boundParams) {
param = boundParams[attribute];
if (isPermissibleBoundValue(this[attribute])) {
params[param] = this[attribute];
}
}
return params;
};
CachedResource.prototype.$$addToCache = function(dirty) {
var entry;
if (dirty == null) {
dirty = false;
}
entry = new ResourceCacheEntry($key, this.$params());
entry.set(this, dirty);
return this;
};
CachedResource.$clearCache = function(arg1) {
var cacheArrayEntry, cacheKeys, clearChildren, clearPendingWrites, entries, exceptFor, isArray, key, params, queue, ref, ref1, translateEntriesToCacheKeys, translateParamsArrayToCacheKeys, translateParamsArrayToEntries, where;
ref = arg1 != null ? arg1 : {}, where = ref.where, exceptFor = ref.exceptFor, clearPendingWrites = ref.clearPendingWrites, isArray = ref.isArray, clearChildren = ref.clearChildren;
if (where == null) {
where = null;
}
if (exceptFor == null) {
exceptFor = null;
}
if (clearPendingWrites == null) {
clearPendingWrites = false;
}
if (isArray == null) {
isArray = false;
}
if (clearChildren == null) {
clearChildren = false;
}
if (where && exceptFor) {
return $log.error("Using where and exceptFor arguments at once in $clearCache() method is forbidden!");
}
cacheKeys = [];
translateParamsArrayToEntries = function(entries) {
entries || (entries = []);
if (!angular.isArray(entries)) {
entries = [entries];
}
return entries.map(function(entry) {
return new CachedResource(entry).$params();
});
};
translateEntriesToCacheKeys = function(params_objects) {
return params_objects.map(function(params) {
return new ResourceCacheEntry($key, params).fullCacheKey();
});
};
translateParamsArrayToCacheKeys = function(entries) {
return translateEntriesToCacheKeys(translateParamsArrayToEntries(entries));
};
if (exceptFor || where) {
if (isArray) {
cacheArrayEntry = new ResourceCacheArrayEntry($key, exceptFor || where).load();
cacheKeys.push(cacheArrayEntry.fullCacheKey());
if (cacheArrayEntry.value && ((exceptFor && !clearChildren) || (where && clearChildren))) {
entries = (function() {
var i, len, ref1, results;
ref1 = cacheArrayEntry.value;
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
params = ref1[i];
results.push(params);
}
return results;
})();
if (entries) {
cacheKeys = cacheKeys.concat(translateEntriesToCacheKeys(entries));
}
}
} else {
cacheKeys = translateParamsArrayToCacheKeys(where || exceptFor);
}
}
if (!clearPendingWrites && !where) {
ref1 = CachedResource.$writes, queue = ref1.queue, key = ref1.key;
cacheKeys.push(key);
entries = queue.map(function(resource) {
return resource.resourceParams;
});
cacheKeys = cacheKeys.concat(translateEntriesToCacheKeys(entries));
} else if (clearPendingWrites && where) {
$log.debug("TODO if clearPendingWrites && where");
}
if (where) {
return Cache.clear({
key: $key,
where: cacheKeys
});
} else {
return Cache.clear({
key: $key,
exceptFor: cacheKeys
});
}
};
CachedResource.$addToCache = function(attrs, dirty) {
return new CachedResource(attrs).$$addToCache(dirty);
};
CachedResource.$addArrayToCache = function(attrs, instances, dirty) {
if (dirty == null) {
dirty = false;
}
instances = instances.map(function(instance) {
return new CachedResource(instance);
});
return new ResourceCacheArrayEntry($key, attrs).addInstances(instances, dirty);
};
CachedResource.$resource = Resource;
CachedResource.$key = $key;
return CachedResource;
})();
CachedResource.$writes = new ResourceWriteQueue(CachedResource, $timeout);
for (actionName in actions) {
actionConfig = actions[actionName];
method = actionConfig.method.toUpperCase();
if (actionConfig.cache !== false) {
handler = method === 'GET' && actionConfig.isArray ? readArrayCache($q, providerParams, actionName, CachedResource, actionConfig) : method === 'GET' ? readCache($q, providerParams, actionName, CachedResource, actionConfig) : method === 'POST' || method === 'PUT' || method === 'DELETE' || method === 'PATCH' ? writeCache($q, providerParams, actionName, CachedResource, actionConfig) : void 0;
CachedResource[actionName] = handler;
if (method !== 'GET') {
CachedResource.prototype["$" + actionName] = handler;
}
} else {
CachedResource[actionName] = Resource[actionName];
CachedResource.prototype["$" + actionName] = Resource.prototype["$" + actionName];
}
}
return CachedResource;
};