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

Commit f6ecf9a

Browse files
committed
fix($resource): Always return a resource instance when calling class methods on resources.
Previously, calling `MyResource.save(myResourceInstance)`returned a promise, in contrast to the docs for `$resource`. However, calling `MyResource.save({name: 'Tobias"})`already correctly returned a resource instance. Fixes #4545. Closes #5061.
1 parent a4e6d96 commit f6ecf9a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ngResource/resource.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ angular.module('ngResource', ['ng']).
439439
}
440440
/* jshint +W086 */ /* (purposefully fall through case statements) */
441441

442-
var isInstanceCall = data instanceof Resource;
442+
var isInstanceCall = this instanceof Resource;
443443
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));
444444
var httpConfig = {};
445445
var responseInterceptor = action.interceptor && action.interceptor.response ||
@@ -522,7 +522,7 @@ angular.module('ngResource', ['ng']).
522522
if (isFunction(params)) {
523523
error = success; success = params; params = {};
524524
}
525-
var result = Resource[name](params, this, success, error);
525+
var result = Resource[name].call(this, params, this, success, error);
526526
return result.$promise || result;
527527
};
528528
});

test/ngResource/resourceSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,18 @@ describe("resource", function() {
533533
expect(person.name).toEqual('misko');
534534
});
535535

536+
it('should return a resource instance when calling a class method with a resource instance', function() {
537+
$httpBackend.expect('GET', '/Person/123').respond('{"name":"misko"}');
538+
var Person = $resource('/Person/:id');
539+
var person = Person.get({id:123});
540+
$httpBackend.flush();
541+
$httpBackend.expect('POST', '/Person').respond('{"name":"misko2"}');
542+
543+
var person2 = Person.save(person);
544+
$httpBackend.flush();
545+
546+
expect(person2).toEqual(jasmine.any(Person));
547+
});
536548

537549
describe('promise api', function() {
538550

0 commit comments

Comments
 (0)