CJ Brassington
CJ Brassington

Reputation: 23

Restangular put() not working on multiple put()'s

Here is the code in question

function putCategory(category) {
        console.log(category.parent_id);
        category.put().then(function (data) {
            console.log(data.parent_id);
            delete category._updating;
            angular.extend(category, data);
            updateCategories();
        }, function (error) {
            delete category._updating;
            category.errors = readError(error);
        });
    }

Here is a brief example of the problem:

When I inspect the console logs, the input data is correct (the first console log), and the second console log is correct the first time, and wrong the second time (and later).

When I look at the network tab, I can see that the outgoing params are also correct the first time, and wrong in the second.

So as best I can tell, the put() call is not working properly, does anyone have any idea why?

Upvotes: 1

Views: 237

Answers (1)

Bengt
Bengt

Reputation: 4038

Restangular does some tricky things with the this binding of the elements (see Copying elements for another problematic scenario). The problem is most likely in your call angular.extend(category, data);. This call copies all enumerable properties (including Restangular methods), while it would be enough to update the fields (e.g. id, name, date) of the element.

The most easy solution for this is to use the plain() function of your response. This function returns the pure data object you received, so you can use it to copy exclusively your data fields.

category.put().then(function (data) {
            console.log(data.parent_id);
            delete category._updating;
            angular.extend(category, data.plain());
            updateCategories();
        });

Upvotes: 2

Related Questions