Skip to content

Commit

Permalink
fix($resource): pass transformed value to both callbacks and promises
Browse files Browse the repository at this point in the history
  • Loading branch information
jankuca authored and jamesdaily committed Jan 27, 2014
1 parent 2b4aff1 commit d409e15
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,6 @@ angular.module('ngResource', ['ng']).

value.$resolved = true;

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

response.resource = value;

return response;
Expand All @@ -503,8 +501,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
34 changes: 33 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,38 @@ 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, { value: 'original' });

var transformResponse = function (response) {
return { value: 'transformed' };
};

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

var successValue,
promiseValue;

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

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

$httpBackend.flush();
expect(successValue).toEqual({ value: 'transformed' });
expect(promiseValue).toEqual({ value: 'transformed' });
expect(successValue).toBe(promiseValue);
});
});


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


});
});

0 comments on commit d409e15

Please sign in to comment.