Update with Resource Angularjs

Asked

Viewed 206 times

0

There is the method Callback to the resource.update in the AngularJS?

Type,

var instancia = Resource.get({id:1}, function(data){
    instancia.$update({id:idModelo}, modeloAtualizado)
});

This works, the record is updated perfectly, however, I would like to treat if the update has occurred successfully or if there has been a failure.

In the other methods, $post, $create etc there is the Callback method, but in $update doesn’t work.

Is there any way to do that?

My Factory Source:

app.factory( "meuResource", function ($resource) {
    return $resource(URL+"meuModelo/:id", null, {
        'update' : { method: "PUT"}, 
        'get' : { method: "GET"},
        'post': { method: 'POST'},
        'delete' : { method: 'DELETE'}        
    });
});

My method of updating:

$scope.edit = function(){
    Resource.get({id:$scope.vm.id}, function(data){
        try{ 
            var instancia = angular.copy($scope.vm);      

            Resource.update({id:instancia.id}, instancia);
        }
        catch(e){
            toastr.error("Não foi possível atualizar o registro!");
        }
    }, function(data){
        toastr.error("Ocorreu uma falha na gravação do registro!", {timeOut:2500});                
        console.log(data);
    });       
}

There is a try within the "get", however, the exception will only be made if there is an error in the inside of the "get" during the run.

Like the "Resource.update({id:instancia.id}, instancia);" is asynchronous, if an error occurs in its execution, this error goes unnoticed.

1 answer

0

Then, after talking with my friend Savio Freitas, he showed me a light.. said and done, it worked.

            Resource.update({id:instancia.id}, instancia, function(data){
                //callback de successo
            }, function(data){
                //callback de error
            });

Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.