Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions angular-cached-resource.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var DEFAULT_ACTIONS, buildCachedResourceClass, readArrayCache, readCache, writeCache;

DEFAULT_ACTIONS = {
Expand Down Expand Up @@ -220,8 +219,8 @@ module.exports = buildCachedResourceClass = function($resource, $timeout, $q, pr
return CachedResource;
};


},{"./cache":2,"./read_array_cache":7,"./read_cache":8,"./resource_cache_array_entry":9,"./resource_cache_entry":10,"./resource_write_queue":11,"./write_cache":12}],2:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var Cache, localStorage;

localStorage = window.localStorage;
Expand All @@ -246,7 +245,7 @@ Cache = (function() {
};

Cache.prototype.setItem = function(key, value) {
var e, error, stringValue;
var e, stringValue;
key = this._buildKey(key);
stringValue = angular.toJson(value);
try {
Expand Down Expand Up @@ -336,8 +335,8 @@ module.exports = function(providerParams) {
return new Cache(providerParams);
};


},{}],3:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

module.exports = function(providerParams) {
Expand Down Expand Up @@ -404,8 +403,8 @@ module.exports = function(providerParams) {
})();
};


},{"./build_cached_resource_class":1,"./cache":2}],4:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var $cachedResourceFactory, $cachedResourceProvider, app, debugMode, localStoragePrefix, resourceManagerListener,
slice = [].slice;

Expand Down Expand Up @@ -481,8 +480,8 @@ $cachedResourceFactory = [
}
];


},{"./cached_resource_manager":3}],5:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var modifyObjectInPlace;

module.exports = modifyObjectInPlace = function(oldObject, newObject, cachedObject) {
Expand Down Expand Up @@ -517,8 +516,8 @@ module.exports = modifyObjectInPlace = function(oldObject, newObject, cachedObje
}
};


},{}],6:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var processReadArgs;

module.exports = processReadArgs = function($q, args) {
Expand All @@ -539,8 +538,8 @@ module.exports = processReadArgs = function($q, args) {
};
};


},{}],7:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var modifyObjectInPlace, processReadArgs, readArrayCache,
slice = [].slice;

Expand Down Expand Up @@ -586,8 +585,7 @@ module.exports = readArrayCache = function($q, providerParams, name, CachedResou
});
readHttp = function() {
var resource;
resource = CachedResource.$resource[name](params);
resource.$promise.then(function(response) {
return resource = CachedResource.$resource[name](params, function(response, headers) {
var newArrayInstance;
newArrayInstance = new Array();
response.map(function(resourceInstance) {
Expand All @@ -602,12 +600,12 @@ module.exports = readArrayCache = function($q, providerParams, name, CachedResou
}
});
arrayInstance.splice.apply(arrayInstance, [0, arrayInstance.length].concat(slice.call(newArrayInstance)));
arrayInstance.headers = headers();
if (!cacheArrayEntry.value) {
cacheDeferred.resolve(arrayInstance);
}
return httpDeferred.resolve(arrayInstance);
});
return resource.$promise["catch"](function(error) {
}, function(error) {
if (!cacheArrayEntry.value) {
cacheDeferred.reject(error);
}
Expand All @@ -618,12 +616,15 @@ module.exports = readArrayCache = function($q, providerParams, name, CachedResou
CachedResource.$writes.flush(readHttp);
}
if (cacheArrayEntry.value) {
ref1 = cacheArrayEntry.value;
ref1 = cacheArrayEntry.value.data;
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));
}
if (cacheArrayEntry.value.headers) {
arrayInstance.headers = cacheArrayEntry.value.headers;
}
cacheDeferred.resolve(arrayInstance);
} else if (actionConfig.cacheOnly) {
cacheDeferred.reject(new Error("Cache value does not exist for params", params));
Expand All @@ -632,8 +633,8 @@ module.exports = readArrayCache = function($q, providerParams, name, CachedResou
};
};


},{"./modify_object_in_place":5,"./process_read_args":6,"./resource_cache_array_entry":9,"./resource_cache_entry":10}],8:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var modifyObjectInPlace, processReadArgs, readCache;

processReadArgs = require('./process_read_args');
Expand Down Expand Up @@ -688,8 +689,8 @@ module.exports = readCache = function($q, providerParams, name, CachedResource,
};
};


},{"./modify_object_in_place":5,"./process_read_args":6,"./resource_cache_entry":10}],9:[function(require,module,exports){
// 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;

Expand Down Expand Up @@ -717,17 +718,21 @@ module.exports = function(providerParams) {
append: false
};
}
cacheArrayReferences = options.append ? this.value : [];
if (cacheArrayReferences == null) {
cacheArrayReferences = [];
cacheArrayReferences = {};
cacheArrayReferences.data = options.append ? this.value : [];
if (cacheArrayReferences.data == null) {
cacheArrayReferences.data = [];
}
if (instances.headers) {
cacheArrayReferences.headers = instances.headers;
}
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);
cacheArrayReferences.data.push(cacheInstanceParams);
cacheInstanceEntry = new ResourceCacheEntry(this.key, cacheInstanceParams).load();
if (!(options.append && (cacheInstanceEntry.value != null))) {
cacheInstanceEntry.set(instance, dirty);
Expand All @@ -742,8 +747,8 @@ module.exports = function(providerParams) {
})(ResourceCacheEntry);
};


},{"./resource_cache_entry":10}],10:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
module.exports = function(providerParams) {
var $log, Cache, ResourceCacheEntry;
$log = providerParams.$log;
Expand Down Expand Up @@ -802,8 +807,8 @@ module.exports = function(providerParams) {
})();
};


},{"./cache":2}],11:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var CACHE_RETRY_TIMEOUT;

CACHE_RETRY_TIMEOUT = 60000;
Expand Down Expand Up @@ -1057,8 +1062,8 @@ module.exports = function(providerParams, $q) {
})();
};


},{"./cache":2,"./resource_cache_entry":10}],12:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
var modifyObjectInPlace, writeCache;

modifyObjectInPlace = require('./modify_object_in_place');
Expand Down Expand Up @@ -1133,4 +1138,5 @@ module.exports = writeCache = function($q, providerParams, action, CachedResourc
};
};

},{"./modify_object_in_place":5,"./resource_cache_entry":10}]},{},[4]);

},{"./modify_object_in_place":5,"./resource_cache_entry":10}]},{},[1,2,3,4,5,6,7,8,9,10,11,12]);
11 changes: 7 additions & 4 deletions src/read_array_cache.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ module.exports = readArrayCache = ($q, providerParams, name, CachedResource, act
cacheArrayEntry.addInstances(instances, false)

readHttp = ->
resource = CachedResource.$resource[name](params)
resource.$promise.then (response) ->
resource = CachedResource.$resource[name](params, (response, headers) ->
newArrayInstance = new Array()

response.map (resourceInstance) ->
Expand All @@ -49,20 +48,24 @@ module.exports = readArrayCache = ($q, providerParams, name, CachedResource, act
newArrayInstance.push resourceInstance

arrayInstance.splice(0, arrayInstance.length, newArrayInstance...)
arrayInstance.headers = headers()

cacheDeferred.resolve arrayInstance unless cacheArrayEntry.value
httpDeferred.resolve arrayInstance
resource.$promise.catch (error) ->
, (error) ->
cacheDeferred.reject error unless cacheArrayEntry.value
httpDeferred.reject error
)

if not actionConfig.cacheOnly
CachedResource.$writes.flush readHttp

if cacheArrayEntry.value
for cacheInstanceParams in cacheArrayEntry.value
for cacheInstanceParams in cacheArrayEntry.value.data
cacheInstanceEntry = new ResourceCacheEntry(CachedResource.$key, cacheInstanceParams).load()
arrayInstance.push new CachedResource cacheInstanceEntry.value
if cacheArrayEntry.value.headers
arrayInstance.headers = cacheArrayEntry.value.headers

# Resolve the promise as the cache is ready
cacheDeferred.resolve arrayInstance
Expand Down
9 changes: 6 additions & 3 deletions src/resource_cache_array_entry.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ module.exports = (providerParams) ->
cacheKeyPrefix: -> "#{@key}/array"

addInstances: (instances, dirty, options = {append: false}) ->
cacheArrayReferences = if options.append then @value else []
cacheArrayReferences ?= []
cacheArrayReferences = {}
cacheArrayReferences.data = if options.append then @value else []
cacheArrayReferences.data ?= []
if instances.headers
cacheArrayReferences.headers = instances.headers

for instance in instances
cacheInstanceParams = instance.$params()
Expand All @@ -17,7 +20,7 @@ module.exports = (providerParams) ->
'#{@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
cacheArrayReferences.data.push cacheInstanceParams
cacheInstanceEntry = new ResourceCacheEntry(@key, cacheInstanceParams).load()
# if we're appending and there's already a resource entry, we won't clobber external intent (eg. $save) about that resource
unless options.append and cacheInstanceEntry.value?
Expand Down