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

Commit 2c7440f

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 }}); ``` Closes #14821
1 parent c434bde commit 2c7440f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/ngResource/resource.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function shallowClearAndCopy(src, dst) {
132132
* Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action
133133
* method that does not accept a request body)
134134
*
135-
* @param {Object.<Object>=} actions Hash with declaration of custom actions that should extend
135+
* @param {Object.<Object>=} actions Hash with declaration of custom actions that should be merged with
136136
* the default set of resource actions. The declaration should be created in the format of {@link
137137
* ng.$http#usage $http.config}:
138138
*
@@ -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)