用户Angular资源服务$ save函数的正确方法

时间:2021-12-09 10:32:09

I'm working with angular resources trying to use the angular $save function.

我正在使用角度资源尝试使用角度$ save函数。

By default $save sends the model back to the service URL. However, it looks like it expects the model to be returned to the service (my model is empty if I don't do this). I was wondering what the best way to return messages and errors to the controller is.

默认情况下,$ save将模型发送回服务URL。但是,看起来它希望模型返回到服务(如果我不这样做,我的模型是空的)。我想知道将消息和错误返回给控制器的最佳方法是什么。

My solution was to create a new class in my PHP that has an errors array that stores any errors encountered in processing and a field that stores the model for returning. It's then send back and processed in the call-back function:

我的解决方案是在我的PHP中创建一个新类,它有一个errors数组,用于存储处理中遇到的任何错误,以及一个存储模型以便返回的字段。然后在回调函数中发回并处理:

$scope.ApplyChanges=function(){
   console.log("saving...");
    $scope.user.$save(function(data){
      console.log(data);
      if (data.errors.length>0){
         for (error in data.errors){
            $scope.alerts.push({type:'danger', msg: data.errors[error].msg});
         }
         $scope.user=User.get({id:data.data.UserName});
      } else {
         $scope.user=User.get({id:data.data.UserName});
         $scope.alerts.push({type:'success', msg: "User data saved successfully"});
      }
    }, function(err){
         $scope.alerts.push({type:'danger', msg: "There was a problem saving your data: " + err});
    });

these lines here: $scope.user=User.get({id:data.data.UserName}); I had to use because if I just assigned my $scope.user to data.data, user was no longer using the service and I would get an error when I tried to ApplyChanges again.

这些行:$ scope.user = User.get({id:data.data.UserName});我不得不使用,因为如果我刚将$ scope.user分配给data.data,则用户不再使用该服务,当我再次尝试ApplyChanges时会出现错误。

So, is there a way to do this more seemlessly? As it is I have to make an additional call back to get the model. Should I send an error only if there's an error and then have an additional callback to get the model? Is there a better way?

那么,有没有办法更无瑕地做到这一点?因为它是我必须额外回电来获得模型。我是否应该仅在出现错误时发送错误,然后再进行回调以获取模型?有没有更好的办法?

1 个解决方案

#1


2  

First of all, your server should return errors with relevant HTTP error status codes (see 4xx and 5xx codes). That way, you only handle errors in the error callback:

首先,您的服务器应返回带有相关HTTP错误状态代码的错误(请参阅4xx和5xx代码)。这样,您只处理错误回调中的错误:

function onError (response){
    switch (response.status) {
    case 400:
    case 404:
    //etc... 
        response.data.errors.forEach(function(error){
            $scope.alerts.push({type:'danger', msg: error.msg});
        });
        break;
    case 500:
        $scope.alerts.push({type:'danger', msg: "There was a problem saving your data: " + response.data});
        break;
    }
}

That said, if $scope.user is a $resource instance, then you do not have to get it again from the server, the $save() method will not change the object.

也就是说,如果$ scope.user是一个$ resource实例,那么你不必再从服务器获取它,$ save()方法不会改变对象。

To copy values from the 'user' object retrieved from the server into the $scope.user just use angular.extend()

要将从服务器检索到的'user'对象中的值复制到$ scope.user,只需使用angular.extend()

angular.extend($scope.user, data) //this updates $scope.user with data attributes.

Is worth noting that angular.extend does not perform a deep copy, if needed, use jQuery.extend:

值得注意的是angular.extend不执行深层复制,如果需要,使用jQuery.extend:

jQuery.extend(true, $scope.user, data)

#1


2  

First of all, your server should return errors with relevant HTTP error status codes (see 4xx and 5xx codes). That way, you only handle errors in the error callback:

首先,您的服务器应返回带有相关HTTP错误状态代码的错误(请参阅4xx和5xx代码)。这样,您只处理错误回调中的错误:

function onError (response){
    switch (response.status) {
    case 400:
    case 404:
    //etc... 
        response.data.errors.forEach(function(error){
            $scope.alerts.push({type:'danger', msg: error.msg});
        });
        break;
    case 500:
        $scope.alerts.push({type:'danger', msg: "There was a problem saving your data: " + response.data});
        break;
    }
}

That said, if $scope.user is a $resource instance, then you do not have to get it again from the server, the $save() method will not change the object.

也就是说,如果$ scope.user是一个$ resource实例,那么你不必再从服务器获取它,$ save()方法不会改变对象。

To copy values from the 'user' object retrieved from the server into the $scope.user just use angular.extend()

要将从服务器检索到的'user'对象中的值复制到$ scope.user,只需使用angular.extend()

angular.extend($scope.user, data) //this updates $scope.user with data attributes.

Is worth noting that angular.extend does not perform a deep copy, if needed, use jQuery.extend:

值得注意的是angular.extend不执行深层复制,如果需要,使用jQuery.extend:

jQuery.extend(true, $scope.user, data)