UNPKG

angular-cached-resource

Version:

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

92 lines (88 loc) 3.63 kB
// Generated by CoffeeScript 1.10.0 var modifyObjectInPlace, processReadArgs, readArrayCache, slice = [].slice; processReadArgs = require('./process_read_args'); modifyObjectInPlace = require('./modify_object_in_place'); module.exports = readArrayCache = function($q, providerParams, name, CachedResource, actionConfig) { var ResourceCacheArrayEntry, ResourceCacheEntry, first; ResourceCacheEntry = require('./resource_cache_entry')(providerParams); ResourceCacheArrayEntry = require('./resource_cache_array_entry')(providerParams); first = function(array, params) { var found, i, item, itemParams, len; found = null; for (i = 0, len = array.length; i < len; i++) { item = array[i]; itemParams = item.$params(); if (Object.keys(params).every(function(key) { return itemParams[key] === params[key]; })) { found = item; break; } } return found; }; return function() { var arrayInstance, cacheArrayEntry, cacheDeferred, cacheInstanceEntry, cacheInstanceParams, httpDeferred, i, len, params, readHttp, ref, ref1; ref = processReadArgs($q, arguments), params = ref.params, cacheDeferred = ref.deferred; httpDeferred = $q.defer(); arrayInstance = new Array(); arrayInstance.$promise = cacheDeferred.promise; arrayInstance.$httpPromise = httpDeferred.promise; cacheArrayEntry = new ResourceCacheArrayEntry(CachedResource.$key, params).load(); arrayInstance.$push = function(resourceInstance) { arrayInstance.push(resourceInstance); return cacheArrayEntry.addInstances([resourceInstance], false, { append: true }); }; arrayInstance.$httpPromise.then(function(instances) { return cacheArrayEntry.addInstances(instances, false); }); readHttp = function() { var resource; resource = CachedResource.$resource[name](params); resource.$promise.then(function(response) { var newArrayInstance; newArrayInstance = new Array(); response.map(function(resourceInstance) { var existingInstance; resourceInstance = new CachedResource(resourceInstance); existingInstance = first(arrayInstance, resourceInstance.$params()); if (existingInstance) { modifyObjectInPlace(existingInstance, resourceInstance); return newArrayInstance.push(existingInstance); } else { return newArrayInstance.push(resourceInstance); } }); arrayInstance.splice.apply(arrayInstance, [0, arrayInstance.length].concat(slice.call(newArrayInstance))); if (!cacheArrayEntry.value) { cacheDeferred.resolve(arrayInstance); } return httpDeferred.resolve(arrayInstance); }); return resource.$promise["catch"](function(error) { if (!cacheArrayEntry.value) { cacheDeferred.reject(error); } return httpDeferred.reject(error); }); }; if (!actionConfig.cacheOnly) { CachedResource.$writes.flush(readHttp); } if (cacheArrayEntry.value) { ref1 = cacheArrayEntry.value; for (i = 0, len = ref1.length; i < len; i++) { cacheInstanceParams = ref1[i]; cacheInstanceEntry = new ResourceCacheEntry(CachedResource.$key, cacheInstanceParams).load(); arrayInstance.push(new CachedResource(cacheInstanceEntry.value)); } cacheDeferred.resolve(arrayInstance); } else if (actionConfig.cacheOnly) { cacheDeferred.reject(new Error("Cache value does not exist for params", params)); } return arrayInstance; }; };