From 8782e44b2c106af5537c2f52663831296014028c Mon Sep 17 00:00:00 2001 From: Guillaume Viguier Date: Fri, 25 Nov 2016 12:07:18 +0100 Subject: [PATCH 1/3] #70 - access HTTP headers from resource --- src/read_array_cache.coffee | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/read_array_cache.coffee b/src/read_array_cache.coffee index ff700be..14105e0 100644 --- a/src/read_array_cache.coffee +++ b/src/read_array_cache.coffee @@ -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) -> @@ -49,12 +48,14 @@ 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 From f06bf83278cc02f9b4f4b3bf10e5b976e430e11d Mon Sep 17 00:00:00 2001 From: Guillaume Viguier Date: Fri, 25 Nov 2016 15:30:28 +0100 Subject: [PATCH 2/3] #70 - put headers in cache --- angular-cached-resource.js | 52 +++++++++++++++------------ src/read_array_cache.coffee | 4 ++- src/resource_cache_array_entry.coffee | 9 +++-- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/angular-cached-resource.js b/angular-cached-resource.js index 259e0af..714b5eb 100644 --- a/angular-cached-resource.js +++ b/angular-cached-resource.js @@ -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 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() @@ -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? From 4e260375e58d64770dc8ef8b1353808fffaf0b00 Mon Sep 17 00:00:00 2001 From: Guillaume Viguier Date: Fri, 25 Nov 2016 16:10:45 +0100 Subject: [PATCH 3/3] #70 - fixed typo --- angular-cached-resource.js | 2 +- src/read_array_cache.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/angular-cached-resource.js b/angular-cached-resource.js index 714b5eb..7bb1e28 100644 --- a/angular-cached-resource.js +++ b/angular-cached-resource.js @@ -600,7 +600,7 @@ module.exports = readArrayCache = function($q, providerParams, name, CachedResou } }); arrayInstance.splice.apply(arrayInstance, [0, arrayInstance.length].concat(slice.call(newArrayInstance))); - arrayInstance.$headers = headers(); + arrayInstance.headers = headers(); if (!cacheArrayEntry.value) { cacheDeferred.resolve(arrayInstance); } diff --git a/src/read_array_cache.coffee b/src/read_array_cache.coffee index 689ca9a..89bd457 100644 --- a/src/read_array_cache.coffee +++ b/src/read_array_cache.coffee @@ -48,7 +48,7 @@ module.exports = readArrayCache = ($q, providerParams, name, CachedResource, act newArrayInstance.push resourceInstance arrayInstance.splice(0, arrayInstance.length, newArrayInstance...) - arrayInstance.$headers = headers() + arrayInstance.headers = headers() cacheDeferred.resolve arrayInstance unless cacheArrayEntry.value httpDeferred.resolve arrayInstance