From 32c35f1a63125ef72800a93fb6caf8236ec12f77 Mon Sep 17 00:00:00 2001 From: Gabriel Monteagudo Date: Mon, 4 Jul 2016 13:43:32 -0300 Subject: [PATCH] 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 --- src/ngResource/resource.js | 5 +++-- test/ngResource/resourceSpec.js | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ngResource/resource.js b/src/ngResource/resource.js index 31a594421e50..5629166f6023 100644 --- a/src/ngResource/resource.js +++ b/src/ngResource/resource.js @@ -132,7 +132,7 @@ function shallowClearAndCopy(src, dst) { * Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action * method that does not accept a request body) * - * @param {Object.=} actions Hash with declaration of custom actions that should extend + * @param {Object.=} actions Hash with declaration of custom actions that should be merged with * the default set of resource actions. The declaration should be created in the format of {@link * ng.$http#usage $http.config}: * @@ -516,6 +516,7 @@ angular.module('ngResource', ['ng']). var noop = angular.noop, forEach = angular.forEach, extend = angular.extend, + merge = angular.merge, copy = angular.copy, isFunction = angular.isFunction, encodeUriQuery = angular.$$encodeUriQuery, @@ -603,7 +604,7 @@ angular.module('ngResource', ['ng']). function resourceFactory(url, paramDefaults, actions, options) { var route = new Route(url, options); - actions = extend({}, provider.defaults.actions, actions); + actions = merge({}, provider.defaults.actions, actions); function extractParams(data, actionParams) { var ids = {}; diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index a848f06d1244..a5b33888744d 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -491,6 +491,18 @@ describe("basic usage", function() { }); + it('should deep-merge custom actions with default actions', function() { + $httpBackend.when('GET', '/foo').respond({invokedMethod: 'get'}); + $httpBackend.when('POST', '/foo').respond({invokedMethod: 'post'}); + + var Resource = $resource('/foo', {}, {save: {timeout: 10000}}); + var response = Resource.save(); + + $httpBackend.flush(); + expect(response.invokedMethod).toEqual('post'); + }); + + it("should read partial resource", function() { $httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]); var ccs = CreditCard.query();