Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($resource): pass interceptor-transformed value to both success callbacks and promises #3823

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 8 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,6 @@ angular.module('ngResource', ['ng']).

value.$resolved = true;

(success||noop)(value, response.headers);

response.resource = value;

return response;
Expand All @@ -502,8 +500,15 @@ angular.module('ngResource', ['ng']).
(error||noop)(response);

return $q.reject(response);
}).then(responseInterceptor, responseErrorInterceptor);
});

promise = promise.then(
function(response) {
var value = responseInterceptor(response);
(success||noop)(value, response.headers);
return value;
},
responseErrorInterceptor);

if (!isInstanceCall) {
// we are creating instance / collection
Expand Down
33 changes: 32 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,37 @@ describe("resource", function() {

expect(cc.url).toBe('/new-id');
});

it('should pass the same transformed value to success callbacks and to promises', function() {
$httpBackend.expect('GET', '/CreditCard').respond(200, { a: 987 });

var transformResponse = function (response) {
return { a: 123 };
};

var CreditCard = $resource('/CreditCard', {}, {
call: {
method: 'get',
interceptor: { response: transformResponse }
}
});

var a, b;
var cc = new CreditCard({name: 'Me'});

var req = cc.$call({}, function (successValue) {
a = successValue;
});
req.then(function (promiseValue) {
b = promiseValue;
});

$httpBackend.flush();
expect(a).toBeDefined();
expect(b).toBeDefined();
expect(a).toBe(b);
expect(cc.$resolved).toBe(true);
});
});


Expand Down Expand Up @@ -1084,4 +1115,4 @@ describe('resource', function() {
});


});
});