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

Commit e36e28e

Browse files
jankucabtford
authored andcommitted
fix($resource): pass transformed value to both callbacks and promises
Closes #3817
1 parent 5b8c788 commit e36e28e

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/ngResource/resource.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,6 @@ angular.module('ngResource', ['ng']).
492492

493493
value.$resolved = true;
494494

495-
(success||noop)(value, response.headers);
496-
497495
response.resource = value;
498496

499497
return response;
@@ -503,8 +501,15 @@ angular.module('ngResource', ['ng']).
503501
(error||noop)(response);
504502

505503
return $q.reject(response);
506-
}).then(responseInterceptor, responseErrorInterceptor);
504+
});
507505

506+
promise = promise.then(
507+
function(response) {
508+
var value = responseInterceptor(response);
509+
(success||noop)(value, response.headers);
510+
return value;
511+
},
512+
responseErrorInterceptor);
508513

509514
if (!isInstanceCall) {
510515
// we are creating instance / collection

test/ngResource/resourceSpec.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,38 @@ describe("resource", function() {
630630

631631
expect(cc.url).toBe('/new-id');
632632
});
633+
634+
it('should pass the same transformed value to success callbacks and to promises', function() {
635+
$httpBackend.expect('GET', '/CreditCard').respond(200, { value: 'original' });
636+
637+
var transformResponse = function (response) {
638+
return { value: 'transformed' };
639+
};
640+
641+
var CreditCard = $resource('/CreditCard', {}, {
642+
call: {
643+
method: 'get',
644+
interceptor: { response: transformResponse }
645+
}
646+
});
647+
648+
var successValue,
649+
promiseValue;
650+
651+
var cc = new CreditCard({ name: 'Me' });
652+
653+
var req = cc.$call({}, function (result) {
654+
successValue = result;
655+
});
656+
req.then(function (result) {
657+
promiseValue = result;
658+
});
659+
660+
$httpBackend.flush();
661+
expect(successValue).toEqual({ value: 'transformed' });
662+
expect(promiseValue).toEqual({ value: 'transformed' });
663+
expect(successValue).toBe(promiseValue);
664+
});
633665
});
634666

635667

@@ -1084,4 +1116,4 @@ describe('resource', function() {
10841116
});
10851117

10861118

1087-
});
1119+
});

0 commit comments

Comments
 (0)