Skip to content

Commit c92b138

Browse files
committed
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.
1 parent fb242ae commit c92b138

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/restangular.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -996,10 +996,31 @@
996996
elem[config.restangularFields.doGETLIST] = elem[config.restangularFields.customGETLIST];
997997
}
998998

999-
function copyRestangularizedElement(fromElement, toElement) {
1000-
var copiedElement = angular.copy(fromElement, toElement);
1001-
return restangularizeElem(fromElement[config.restangularFields.parentResource],
1002-
copiedElement, fromElement[config.restangularFields.route], fromElement[config.restangularFields.fromServer]);
999+
function copyRestangularizedElement(element) {
1000+
var copiedElement = angular.copy(element);
1001+
1002+
// check if we're dealing with a collection (i.e. an array)
1003+
// and restangularize the element using the proper restangularizer,
1004+
// element / collection
1005+
if (_.isArray(element)) {
1006+
return restangularizeCollection(
1007+
element[config.restangularFields.parentResource],
1008+
copiedElement,
1009+
element[config.restangularFields.route],
1010+
element[config.restangularFields.fromServer],
1011+
element[config.restangularFields.reqParams]
1012+
);
1013+
}
1014+
1015+
// not a collection, restangularize it as an element
1016+
return restangularizeElem(
1017+
element[config.restangularFields.parentResource],
1018+
copiedElement,
1019+
element[config.restangularFields.route],
1020+
element[config.restangularFields.fromServer],
1021+
element[config.restangularFields.restangularCollection],
1022+
element[config.restangularFields.reqParams]
1023+
);
10031024
}
10041025

10051026
function restangularizeElem(parent, element, route, fromServer, collection, reqParams) {

test/restangularSpec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,24 @@ describe('Restangular', function() {
11051105
$httpBackend.flush();
11061106
});
11071107

1108+
it('should work with cloned collections', function () {
1109+
var responseHandler = jasmine.createSpy();
1110+
1111+
Restangular.addElementTransformer(/^accounts/, true, function(collection) {
1112+
collection.customThing = 'customValue';
1113+
return collection;
1114+
});
1115+
1116+
Restangular.all('accounts').getList().then(responseHandler);
1117+
$httpBackend.flush();
1118+
1119+
var accounts = responseHandler.calls[0].args[0];
1120+
var accountsCopy = accounts.clone();
1121+
1122+
expect(accounts.customThing).toEqual('customValue');
1123+
expect(accountsCopy.customThing).toEqual('customValue');
1124+
});
1125+
11081126
it('should allow for a custom method to be placed at the model level using regexp route when one model is requested', function() {
11091127
var accountPromise;
11101128

0 commit comments

Comments
 (0)