Question

I am trying to make a PUT request using RESTAngular. I am fairly new to Angular as well as RESTAngular.

Following is code snippet which works.

$scope.itemToUpdate = Restangular.all($scope.slug);
$scope.itemToUpdate.getList().then(function(items){
    var item = items.one($routeParams.id);
    item.name = $scope.singular.name;
    item.description = $scope.singular.description;
    item.put();
});

This doesn't work.

    $scope.itemToUpdate = Restangular.all($scope.slug);
    $scope.itemToUpdate.getList().then(function(items){
        var item = items.one($routeParams.id);
        item = $scope.singular;
        item.put();
    });

Don't know what am I doing wrong.

$scope.singular gets it data initially as following. Restangular.one('roles', $routeParams.id).getList().$object.

Basically idea is to update this model from form and also prepopulate the form with relevant data when slug matches the id. I can change the way things are wired up if required. So feel free to suggest best practices.

Edit 2

This official demo is very helpful in solving the issue. http://plnkr.co/edit/d6yDka?p=preview

Was it helpful?

Solution

When Restangular returns resouce array\object it adds some methods on the object such as put which has been wired up to update the object on put call to server.

In second case you are assigning item=$scope.singular. $scope.singular may not be a Restangular object and hence does not work.

OTHER TIPS

This official demo is very helpful in solving the issue. http://plnkr.co/edit/d6yDka?p=preview

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top