Skip to content

Commit 8246c36

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 angular#14821
1 parent 6a219ad commit 8246c36

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
encodeUriQuery = angular.$$encodeUriQuery,
@@ -603,7 +604,7 @@ angular.module('ngResource', ['ng']).
603604
function resourceFactory(url, paramDefaults, actions, options) {
604605
var route = new Route(url, options);
605606

606-
actions = extend({}, provider.defaults.actions, actions);
607+
actions = merge({}, provider.defaults.actions, actions);
607608

608609
function extractParams(data, actionParams) {
609610
var ids = {};

test/ngResource/resourceSpec.js

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

493493

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

0 commit comments

Comments
 (0)