restangular icon indicating copy to clipboard operation
restangular copied to clipboard

Restangular.copy causing the route in the object to become 'undefined'

Open mohuk opened this issue 12 years ago • 10 comments

Hey guys

I am trying to make a PUT request by copying the original restangular object, editing its properties and calling put.

But when I do a restangular.copy, it does not make a correct copy of the original. I was able to figure out that it sets the 'route' property to undefined.

See the fiddle below, it demonstrates the copy behavior.

http://jsfiddle.net/uma1rkhan/HB7LU/4678/

I dont know if this is correct or not but I am not able to make a successful PUT request with the copied object.

thanks :)

mohuk avatar Jul 01 '14 07:07 mohuk

I have this problem as well. I'm using .one() to get a single record and use .put() on that inside the promise.

It works if I manually add: obj.route = 'aRoute'; before calling .put()

Saucy avatar Nov 04 '14 15:11 Saucy

I experienced this same issue with copying collections. It seems the copyRestangularizedElement() function is either broken or I do not understand what it is attempting to do. This is exposed as Restangular.copy() externally. It is referencing the copied element to get the parent and the route after an angular.copy() call, but copying an array will disregard extra properties on an array and so they will always be undefined. My work-around has been to override the Restangular.copy() method to the following:

Restangular.copy = function (source, target) {
    var element = angular.copy(source, target);
    var parent = source[this.configuration.restangularFields.parentResource];
    var route = source[this.configuration.restangularFields.route];

    if (angular.isArray(element)) {
        return Restangular.restangularizeCollection(parent, element, route, true);
    }

    return Restangular.restangularizeElem(parent, element, route, true);
}

I would really like to know if I am just misunderstanding the intended behavior of the copyRestangularizedElement() function. I am relatively new to Restangular so I am sure there is still loads I do not know.

astephens25 avatar Nov 13 '14 16:11 astephens25

+1, reqParams is also undefined after copy. Maybe other stuff too...

philippeluickx avatar Aug 21 '15 14:08 philippeluickx

+1, Restangular.copy() on a collection breaks the collection. If I do a collectionName.post(newObj) after the copy(), there is an extra "/[object%20Object]" added to the route.

Using the code above fixes at least that issue.

rnybergh avatar Oct 06 '15 16:10 rnybergh

+1

rnybergh avatar Jan 11 '16 06:01 rnybergh

+1

akaNightmare avatar Aug 29 '16 12:08 akaNightmare

Yup this is definitely broken. Confirmed with the latest version of Restangular:

http://plnkr.co/edit/zDuP4zWH6fD4ATSxPb03?p=preview

If someone wants to track this down and submit a PR, that would be awesome.

daviesgeek avatar Aug 31 '16 02:08 daviesgeek

It's been a while.

mohuk avatar Sep 30 '16 05:09 mohuk

Is this closed because its resolved? I get the same issue using restangular 1.5.2, where the copy is not done properly. I followed the plunker example. (http://plnkr.co/edit/d6yDka?p=preview)

original - resolves from the project using resolve in routes. (Plunker line 51 of app.js) { "_id":{ "$oid":"57f2a51ebd966f4fb6b51548" }, "name":"Sample 2", "description":"asdas so this is important and its using schema form.", "site":"http://www.google.com", "route":"projects", "reqParams":{ "apiKey":"4f847ad3e4b08a2eed5f3b54" }, "restangularized":true, "fromServer":true, "parentResource":null, "restangularCollection":false }

Using Restangular.copy( of original above) { "_id":{ "$oid":"57f2a51ebd966f4fb6b51548" }, "name":"Sample 2", "description":"asdas so this is important and its using schema form.", "site":"http://www.google.com", "route":"projects", "reqParams":null, "restangularized":true, "fromServer":true, "parentResource":null, "restangularCollection":false }

Notice that the reqParams: is null in the second case. Please do mind, i am using restangular version 1.5.2 installed through bower install.

rafialikhan avatar Oct 03 '16 20:10 rafialikhan

Hi, Still happening on 1.6.1

http://plnkr.co/edit/gwSsimaEDmC6IJHorcNA?p=preview

Regards, Andy

ap1969 avatar Jun 27 '17 21:06 ap1969