从要求上$返回的项目prevent ngResource保存 [英] Prevent ngResource from requiring a returned item on $save

查看:117
本文介绍了从要求上$返回的项目prevent ngResource保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来,当我$保存或使用自定义$更新我的资源,它预计要返回的新发布的文件(放或交的)方法。我不想回到从我保存/后OPS什么,但声明的OK / 200 /错误类型,但这样做会导致该项目尽快保存的空​​白。

  VAR项目= Things.get({
 ID:$ route.current.params.id
},函数(){
 的console.log(项目); //项目是确定
});VAR项目= Things.get({
 ID:$ route.current.params.id
},函数(){
 的console.log(项目); //项目是空白的(因为服务器返回的只是'OK')
 。项目$保存();
});


解决方案

当你做了 $保存()会有的验证以检查是否有任何数据返回,如果多数民众赞成的情况下,响应将是复制到原始对象(在你的情况项目

这是接收来自服务器的响应时回调:

  VAR承诺= $ HTTP(httpConfig)。然后(功能(响应){
  VAR数据= response.data,
      。承诺= $价值承诺;  如果(数据){
    //需要action.isArray转换的情况下,为布尔它是未定义
    // jshint -W018
    如果(angular.isArray(数据)!==(!! action.isArray)){
      扔'在资源配置中的错误。预计'$ resourceMinErr('badcfg',+
        响应包含{0},但得到的{1},
        ?action.isArray数组:对象,angular.isArray(数据)数组:对象);
    }
    // jshint + W018
    如果(action.isArray){
      value.length = 0;
      的forEach(数据,功能(项目){
        value.push(新资源(项目));
      });
    }其他{
      shallowClearAndCopy(数据值);
      。价值$承诺=承诺;
    }
  }  。价值$解决= TRUE;  response.resource =价值;  返回响应;
},函数(响应){
  。价值$解决= TRUE;  (错误|| NOOP)(响应);  返回$ q.reject(响应);
});

您可以查看完整的源$ C ​​$ C

所以,如果你不希望你的对象覆盖做,那么就不要把从服务器的响应的任何数据。

It seems that when I $save or use a custom $update (put or post) method on my resource that it expects the newly posted document to be returned. I don't want to return anything from my save/post ops but an ok/200/error type of statement but doing so causes the item to be blank as soon as saved.

var item = Things.get({
 id: $route.current.params.id
}, function() {
 console.log(item);  // Item is ok
}); 

var item = Things.get({
 id: $route.current.params.id
}, function() {
 console.log(item);  // Item is blank (because server returned just 'ok')
 item.$save();
}); 

解决方案

When you do a $save() there will be a validation to check if there is any data returned and if thats the case, the response will be copied to the original object (in your case item)

This is the callback when receiving a response from server:

var promise = $http(httpConfig).then(function(response) {
  var data = response.data,
      promise = value.$promise;

  if (data) {
    // Need to convert action.isArray to boolean in case it is undefined
    // jshint -W018
    if (angular.isArray(data) !== (!!action.isArray)) {
      throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
        'response to contain an {0} but got an {1}',
        action.isArray?'array':'object', angular.isArray(data)?'array':'object');
    }
    // jshint +W018
    if (action.isArray) {
      value.length = 0;
      forEach(data, function(item) {
        value.push(new Resource(item));
      });
    } else {
      shallowClearAndCopy(data, value);
      value.$promise = promise;
    }
  }

  value.$resolved = true;

  response.resource = value;

  return response;
}, function(response) {
  value.$resolved = true;

  (error||noop)(response);

  return $q.reject(response);
});

You can check full source code here

So if you don't want to have your object overwritten, then just don't send any data on the response from server.

这篇关于从要求上$返回的项目prevent ngResource保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆