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

Commit 3e2bfd4

Browse files
committed
feat($resource): deep-merge custom actions with default actions.
Previously, if you wanted to specify a timeout, transform, interceptor, or any other property for an action, you also had to include the other properties of the default action (method, isArray): ``` var Resource = $resource('/foo', {}, { save: { method: 'POST', timeout: 10000 }}); ``` Now, a deep-merge between the provided custom actions and the default actions will be performed, so you can specify just the properties you want to add or override: ``` var Resource = $resource('/foo', {}, { save: { timeout: 10000 }}); ```
1 parent c434bde commit 3e2bfd4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/ngResource/resource.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ angular.module('ngResource', ['ng']).
516516
var noop = angular.noop,
517517
forEach = angular.forEach,
518518
extend = angular.extend,
519+
merge = angular.merge,
519520
copy = angular.copy,
520521
isFunction = angular.isFunction;
521522

@@ -640,7 +641,7 @@ angular.module('ngResource', ['ng']).
640641
function resourceFactory(url, paramDefaults, actions, options) {
641642
var route = new Route(url, options);
642643

643-
actions = extend({}, provider.defaults.actions, actions);
644+
actions = merge({}, provider.defaults.actions, actions);
644645

645646
function extractParams(data, actionParams) {
646647
var ids = {};

test/ngResource/resourceSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,19 @@ describe("basic usage", function() {
487487
});
488488

489489

490+
it('should deep-merge custom actions with default actions', function() {
491+
$httpBackend.when('GET', '/foo').respond({invokedMethod: 'get'});
492+
$httpBackend.when('POST', '/foo').respond({invokedMethod: 'post'});
493+
494+
var Resource = $resource('/foo', {}, {save: {timeout: 10000}});
495+
var resource = new Resource();
496+
var response = Resource.save();
497+
498+
$httpBackend.flush();
499+
expect(response.invokedMethod).toEqual('post');
500+
});
501+
502+
490503
it("should read partial resource", function() {
491504
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
492505
var ccs = CreditCard.query();

0 commit comments

Comments
 (0)