From 4d5c10f552d6f0c4bf78eae99a0c8ec71d31b6c7 Mon Sep 17 00:00:00 2001 From: WhiteTrefoil Date: Thu, 11 Sep 2014 23:22:27 +0800 Subject: [PATCH 1/3] Added test for #728 --- test/restangularSpec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/restangularSpec.js b/test/restangularSpec.js index ae771da2..e39147ab 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -557,6 +557,20 @@ describe("Restangular", function() { $httpBackend.flush(); }); + + it("putElement() should pass a restangularCollection to the callback", function() { + $httpBackend.expectGET('/accounts'); + restangularAccounts.getList().then(function(accounts) { + expect(accounts.restangularCollection).toBe(true); + accounts[1].amount += 1; + $httpBackend.expectPUT('/accounts/1'); + accounts.putElement(1).then(function(newAccounts) { + expect(newAccounts.restangularCollection).toBe(true); + expect(newAccounts.putElement).toBeDefined(); + }); + }); + $httpBackend.flush(); + }) }); describe("Scoped Service", function() { From d465326657be204a828c5e2f4ad0342969e293b8 Mon Sep 17 00:00:00 2001 From: WhiteTrefoil Date: Fri, 12 Sep 2014 09:53:38 +0800 Subject: [PATCH 2/3] Make the `.putElement()` to give a restangularCollection --- src/restangular.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/restangular.js b/src/restangular.js index 28e73584..22084d35 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -932,8 +932,13 @@ module.provider('Restangular', function() { function copyRestangularizedElement(fromElement, toElement) { var copiedElement = angular.copy(fromElement, toElement); - return restangularizeElem(copiedElement[config.restangularFields.parentResource], - copiedElement, copiedElement[config.restangularFields.route], true); + if (fromElement.restangularCollection === true) { + return restangularizeCollection(copiedElement[config.restangularFields.parentResource], + copiedElement, copiedElement[config.restangularFields.route], true); + } else { + return restangularizeElem(copiedElement[config.restangularFields.parentResource], + copiedElement, copiedElement[config.restangularFields.route], true); + } } function restangularizeElem(parent, element, route, fromServer, collection, reqParams) { From e4a34840effb7a5bced6d67ec1337ae3932ffb0e Mon Sep 17 00:00:00 2001 From: WhiteTrefoil Date: Fri, 12 Sep 2014 10:06:22 +0800 Subject: [PATCH 3/3] Make the tests and exam more robust. --- src/restangular.js | 2 +- test/restangularSpec.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/restangular.js b/src/restangular.js index 22084d35..bd6fce77 100644 --- a/src/restangular.js +++ b/src/restangular.js @@ -932,7 +932,7 @@ module.provider('Restangular', function() { function copyRestangularizedElement(fromElement, toElement) { var copiedElement = angular.copy(fromElement, toElement); - if (fromElement.restangularCollection === true) { + if (fromElement.restangularCollection != null && fromElement.restangularCollection === true) { return restangularizeCollection(copiedElement[config.restangularFields.parentResource], copiedElement, copiedElement[config.restangularFields.route], true); } else { diff --git a/test/restangularSpec.js b/test/restangularSpec.js index e39147ab..47858004 100644 --- a/test/restangularSpec.js +++ b/test/restangularSpec.js @@ -567,6 +567,8 @@ describe("Restangular", function() { accounts.putElement(1).then(function(newAccounts) { expect(newAccounts.restangularCollection).toBe(true); expect(newAccounts.putElement).toBeDefined(); + expect(newAccounts[0].restangularCollection).toBe(false); + expect(newAccounts[1].restangularCollection).toBe(false); }); }); $httpBackend.flush();