From c92b1388a70bdad3e1cf392aa9f5a7f8e1611b5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Bostr=C3=B6m?= Date: Thu, 5 Jan 2017 10:54:29 +0200 Subject: [PATCH] fix(copy) Collections are now copied/cloned properly Collection elementTransformers didn't get triggered earlier, as restangularizeElem was used for both elements and collections. Now restangularizeCollection is used when copying collections. --- src/restangular.js | 29 +++++++++++++++++++++++++---- test/restangularSpec.js | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/restangular.js b/src/restangular.js index f0aa392d..644a2b8b 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -996,10 +996,31 @@ elem[config.restangularFields.doGETLIST] = elem[config.restangularFields.customGETLIST]; } - function copyRestangularizedElement(fromElement, toElement) { - var copiedElement = angular.copy(fromElement, toElement); - return restangularizeElem(fromElement[config.restangularFields.parentResource], - copiedElement, fromElement[config.restangularFields.route], fromElement[config.restangularFields.fromServer]); + function copyRestangularizedElement(element) { + var copiedElement = angular.copy(element); + + // check if we're dealing with a collection (i.e. an array) + // and restangularize the element using the proper restangularizer, + // element / collection + if (_.isArray(element)) { + return restangularizeCollection( + element[config.restangularFields.parentResource], + copiedElement, + element[config.restangularFields.route], + element[config.restangularFields.fromServer], + element[config.restangularFields.reqParams] + ); + } + + // not a collection, restangularize it as an element + return restangularizeElem( + element[config.restangularFields.parentResource], + copiedElement, + element[config.restangularFields.route], + element[config.restangularFields.fromServer], + element[config.restangularFields.restangularCollection], + element[config.restangularFields.reqParams] + ); } function restangularizeElem(parent, element, route, fromServer, collection, reqParams) { diff --git a/test/restangularSpec.js b/test/restangularSpec.js index 354c630e..a6a08221 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -1105,6 +1105,24 @@ describe('Restangular', function() { $httpBackend.flush(); }); + it('should work with cloned collections', function () { + var responseHandler = jasmine.createSpy(); + + Restangular.addElementTransformer(/^accounts/, true, function(collection) { + collection.customThing = 'customValue'; + return collection; + }); + + Restangular.all('accounts').getList().then(responseHandler); + $httpBackend.flush(); + + var accounts = responseHandler.calls[0].args[0]; + var accountsCopy = accounts.clone(); + + expect(accounts.customThing).toEqual('customValue'); + expect(accountsCopy.customThing).toEqual('customValue'); + }); + it('should allow for a custom method to be placed at the model level using regexp route when one model is requested', function() { var accountPromise;