gpt4 book ai didi

javascript - Breeze 使用 Angular $http 拦截器

转载 作者:数据小太阳 更新时间:2023-10-29 05:55:02 26 4
gpt4 key购买 nike

我使用 Angular $http 拦截器来检查 ajax 请求是否返回 401(未通过身份验证)。如果响应为 401,则原始请求排队,显示登录表单,登录成功后,它会重试排队的请求。这已经适用于 $http, Angular 拦截器的来源是:

define('common.service.security.interceptor', ['angular'], function() {
'use strict';

angular.module('common.service.security.interceptor', ['common.service.security.retryQueue'])

.factory('securityInterceptor', [
'$injector',
'$location',
'securityRetryQueue',
function($injector, $location, securityRetryQueue) {

return function(promise) {

var $http = $injector.get('$http');


// catch the erroneous requests
return promise.then(null, function(originalResponse){
if(originalResponse.status === 401){
promise = securityRetryQueue.pushRetryFn('Unauthorized', function retryRequest(){
return $injector.get('$http')(originalResponse.config);
});
}

return promise;
});
};
}
])

// register the interceptor to the angular http service. method)
.config(['$httpProvider', function($httpProvider) {
$httpProvider.responseInterceptors.push('securityInterceptor');

}]);});

如何使用这个 Angular $http 拦截器发出 Breeze 请求?

Breeze 在文件“Breeze/Adapters/breeze.ajax.angular.js”中为 Angular $http 服务提供了一个包装器。所以第一个想法是告诉 Breeze 使用它:

breeze.config.initializeAdapterInstance("ajax", "angular", true);

调试angular.js,可以看出breeze现在实际上使用了$http,但是并没有执行上面注册的拦截器。在$http里面,有一个数组“reversedInterceptors”,里面存放着注册的拦截器。我将这个数组记录到控制台。如果我使用 $http,这个数组的长度是一个(如预期的那样),但是当用 Breeze 发出请求时,这个数组是空的。

问题是,我如何将这个 $http 拦截器用于 breeze 请求?

这里是breeze.ajax.angular.js的代码,由breeze提供

define('breeze.ajax.angular.module', ['breeze', 'angular'], function (breeze) {
'use strict';
/* jshint ignore:start */
var core = breeze.core;

var httpService;
var rootScope;

var ctor = function () {
this.name = "angular";
this.defaultSettings = {};
};

ctor.prototype.initialize = function () {

var ng = core.requireLib("angular");

if (ng) {
var $injector = ng.injector(['ng']);
$injector.invoke(['$http', '$rootScope',
function (xHttp, xRootScope) {
httpService = xHttp;
rootScope = xRootScope;
}]);
}

};

ctor.prototype.setHttp = function (http) {
httpService = http;
rootScope = null; // to suppress rootScope.digest
};

ctor.prototype.ajax = function (config) {
if (!httpService) {
throw new Error("Unable to locate angular for ajax adapter");
}
var ngConfig = {
method: config.type,
url: config.url,
dataType: config.dataType,
contentType: config.contentType,
crossDomain: config.crossDomain
}

if (config.params) {
// Hack: because of the way that Angular handles writing parameters out to the url.
// so this approach takes over the url param writing completely.
// See: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
var delim = (ngConfig.url.indexOf("?") >= 0) ? "&" : "?";
ngConfig.url = ngConfig.url + delim + encodeParams(config.params);
}

if (config.data) {
ngConfig.data = config.data;
}

if (!core.isEmpty(this.defaultSettings)) {
var compositeConfig = core.extend({}, this.defaultSettings);
ngConfig = core.extend(compositeConfig, ngConfig);
}

httpService(ngConfig).success(function (data, status, headers, xconfig) {
// HACK: because $http returns a server side null as a string containing "null" - this is WRONG.
if (data === "null") data = null;
var httpResponse = {
data: data,
status: status,
getHeaders: headers,
config: config
};
config.success(httpResponse);
}).error(function (data, status, headers, xconfig) {
var httpResponse = {
data: data,
status: status,
getHeaders: headers,
config: config
};
config.error(httpResponse);
});
rootScope && rootScope.$digest();
};

function encodeParams(obj) {
var query = '';
var key, subValue, innerObj;

for (var name in obj) {
var value = obj[name];

if (value instanceof Array) {
for (var i = 0; i < value.length; ++i) {
subValue = value[i];
fullSubName = name + '[' + i + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += encodeParams(innerObj) + '&';
}
} else if (value instanceof Object) {
for (var subName in value) {
subValue = value[subName];
fullSubName = name + '[' + subName + ']';
innerObj = {};
innerObj[fullSubName] = subValue;
query += encodeParams(innerObj) + '&';
}
} else if (value !== undefined) {
query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
}
}

return query.length ? query.substr(0, query.length - 1) : query;
}


breeze.config.registerAdapter("ajax", ctor);

breeze.config.initializeAdapterInstance("ajax", "angular", true);
/* jshint ignore:end */
});

最佳答案

使用 setHttp 方法可以让我将 http 拦截器与 breeze angular ajax 适配器一起使用。在我的环境中,它看起来像这样:

(function() {
'use strict';

var serviceId = 'entityManagerFactory';
angular.module('app').factory(serviceId, ['$http', emFactory]);

function emFactory($http) {

var instance = breeze.config.initializeAdapterInstance("ajax", "angular");
instance.setHttp($http);

...

}
})();

我真正找到任何相关信息的唯一地方是在 download 上的 1.4.4 发行说明中。页。我真的不明白这是做什么的。我相信其中一个 Breeze 家伙会有更好的解释。

关于javascript - Breeze 使用 Angular $http 拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21327238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com