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

Commit

Permalink
fix($resource): Always return a resource instance when calling class …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
tbosch committed Nov 21, 2013
1 parent a4e6d96 commit f6ecf9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ angular.module('ngResource', ['ng']).
}
/* jshint +W086 */ /* (purposefully fall through case statements) */

var isInstanceCall = data instanceof Resource;
var isInstanceCall = this instanceof Resource;
var value = isInstanceCall ? data : (action.isArray ? [] : new Resource(data));
var httpConfig = {};
var responseInterceptor = action.interceptor && action.interceptor.response ||
Expand Down Expand Up @@ -522,7 +522,7 @@ angular.module('ngResource', ['ng']).
if (isFunction(params)) {
error = success; success = params; params = {};
}
var result = Resource[name](params, this, success, error);
var result = Resource[name].call(this, params, this, success, error);
return result.$promise || result;
};
});
Expand Down
12 changes: 12 additions & 0 deletions test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,18 @@ describe("resource", function() {
expect(person.name).toEqual('misko');
});

it('should return a resource instance when calling a class method with a resource instance', function() {
$httpBackend.expect('GET', '/Person/123').respond('{"name":"misko"}');
var Person = $resource('/Person/:id');
var person = Person.get({id:123});
$httpBackend.flush();
$httpBackend.expect('POST', '/Person').respond('{"name":"misko2"}');

var person2 = Person.save(person);
$httpBackend.flush();

expect(person2).toEqual(jasmine.any(Person));
});

describe('promise api', function() {

Expand Down

0 comments on commit f6ecf9a

Please sign in to comment.