Update with Resource AngularJS

0

Is the Callback method for resource.update on AngularJS ?

Type,

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

This works, the registry is updated perfectly, however, I would like to handle whether the update occurred successfully or if it failed.

In other methods, $post , $create etc exists the Callback method, however, in $update does not work.

Is there any way to do this?

My factory resource:

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

My upgrade method:

$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 " get ", however, the exception will only be thrown if there is an error within " get " during execution.

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

    
asked by anonymous 29.03.2017 / 19:21

1 answer

0

So, after talking to my friend Sávio Freitas, the same one 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!

    
29.03.2017 / 20:15