递归拦截$ http [英] Recursively intercepting $http

查看:41
本文介绍了递归拦截$ http的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地使用启用了hateoas的rest api,我想到了拦截HTTP调用并在返回资源对象之前向其资源对象添加一些方法的想法.

In order to better consume a hateoas enabled rest api I got the idea to intercept http calls and add some methods to my resource objects before returning them.

这个想法是,如果资源具有链接数组,我将添加方法以简化后续的HTTP请求.

The idea is that if the resource has an array of links I'll add methods to easy further http requests.

下面的代码被推送到$ httpProvider的拦截器数组,几乎可以完成我想要的事情.

The below code is push to the interceptors array of a $httpProvider and does pretty much what I want.

define(['angular'], function (angular) {

    var $http = angular.injector(['ng']).get('$http');

    function flattenLinks(links) {
        for (var key in links) {
            var rel = links[key].rel;
            var href = links[key].href;
            links[rel] = href;
        }
    }

    return {
        'response': function responseHandler(response) {
            var data = response.data ? response.data : response;
            if(typeof data === 'string' || !data.hasOwnProperty('links')) {
                return response;
            }

            if(data.links instanceof Array) {
                flattenLinks(data.links);
            }

            if(data instanceof Array) {
                for(var key in data) {
                    responseHandler(data[key]);
                }
            }

            data.hasLink = function(link) {
                return link in data.links;
            };

            data.get = function(rel) {
                return $http.get(data.links[rel]);
            };

            data.delete = function(rel) {
                return $http.delete(data.links[rel]);
            };

            data.post = function(rel) {
                return $http.post(data.links[rel], data);
            };

            data.put = function(rel) {
                return $http.put(data.links[rel], data);
            };

            return response;
        }
    };
});

问题是,如下所述,当我使用添加的方法来请求响应时,拦截器未处理我添加的方法来请求响应.在我的拦截器中完成的$ http.get,$ http.delete等不会被拦截(自然!).

The problem is that when I use, as seen below, my added methods to do requests the response isn't handled by my interceptor. The $http.get, $http.delete, etc. done from within my interceptor isn't intercepted (naturally!).

    vm.someResourceObj.get('self').then(function(response) {
        console.log(response);
    });

问题是.如何处理对$ http的内部调用?

So the question is. How do I get the internal calls to $http handled?

推荐答案

使用 $ injector 来获取 $ http ,而不是通过 angular访问它..

Use $injector to get $http instead of accessing it via angular..

详细答案:问题是您如何注入 $ http .看来您得到了一些 $ http 在您的应用程序之外(尽管我不能确切地说出原因). var $ http = angular.injector(['ng']).get('$ http'); 不能完成您想要的(或您认为的工作).

Detailed Answer: The problem is how you inject $http. It seems that you get some $http that is outside of your app (I can't exactly say why, though). var $http = angular.injector(['ng']).get('$http'); doesn't do what you want (or what you think it does).

您可以执行 var $ http = angular.element(document).injector().get('$ http'); (或者最好使用 $ document 在那里),但imo您应该正常使用 $ injector -service:您只需将 $ injector 添加到依赖项/注入语句中,并在拦截器内部使用它来检索您想要的服务: var $ http = $ injector.get("$ http");

You could do var $http = angular.element(document).injector().get('$http'); (or probably better use $document there), but imo you should use the $injector-service normally: you can just add $injector to your dependencies/inject statement and inside your interceptor use that to retrieve the service you want like: var $http = $injector.get("$http");

这样,您的拦截器将拦截它发出的 $ http -调用.

With that your interceptor will intercept the $http-calls it makes.

这篇关于递归拦截$ http的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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