简体   繁体   English

如何使用responseError拦截器在ngResource中获取原始资源?

[英]How can I get the original resource in ngResource with a responseError interceptor?

I am doing some caching of original (pre-send) and new (post-send) data on an ngResource. 我正在ngResource上缓存原始(发送前)和新(发送后)数据。 I am using $resource interceptor for response and responseError . 我正在使用$resource拦截器来responseresponseError

Here is the problem: in response , the argument has a property resource , which allows me to manipulate the resource before passing it back to the caller. 这就是问题所在:作为response ,参数具有属性resource ,这使我可以在将资源传递回调用方之前对其进行操作。

In responseError , there is no such property, so how do I manipulate the resource? responseError ,没有这样的属性,那么如何处理资源?

Code sample: 代码示例:

              update: { method: 'put', isArray: false, interceptor: {
                    response: function (response) {
                        // clear my pristine cache 
                        // I have access to response.resource
                        angular.copy(pristineData,response.resource);
                        return(response);
                    },
                    responseError: function (response) {
                                                    // the PUT failed, I want to reset the data
                        // need to reset the data to pristine
                        // "pristineData" is cached elsewhere
                        // HOW DO I DO THIS, SINCE response.resource UNAVAILABLE?
                        angular.extend(response.resource,pristineData);
                    }
                }},

There was no really good answer, so here is what I did. 没有一个很好的答案,所以这就是我所做的。

I created my own $save which called either $create or $update , and then used promise handlers to do the changes. 我创建了自己的$save ,称为$create$update ,然后使用promise处理程序进行更改。

        factory('Resource',['$resource','_','$q',function ($resource,_,$q) {
        return function(url,params,methods){
            var defaults = {
              update: { method: 'put', isArray: false, interceptor: {
                    response: function (response) {
                        // do pristine cache setting here
                        return(response);
                    }
                }},
              create: { method: 'post',interceptor: {
                    response: function (response) {
                        // do pristine cache setting here
                        return(response);
                    }
              }},
            }, resource = $resource(url,params,angular.extend(defaults,methods));

            // need to change $save *after* creating $resource
            resource.prototype.$save = function() {
                var that = this;
                return this.id ? this.$update.apply(this,arguments).then(null,function (result) {
                    // reset from pristine cache here
                }) : this.$create.apply(this,arguments);
            };
    }]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM