我有一个工厂,它返回一个具有多个属性的对象。但是每个属性值都是通过ajax调用计算的,在某些情况下,我确实会为了设置属性值而保证链接。在我返回对象之前,我如何确保完成所有ajax调用,以便分配属性值
我的工厂看起来像这样
app.factory('Resource', ['$http', '$q', function ($http, $q) {
var Resource = {
masterDB: null,
replicaDB: null,
replicaCluster: null,
masterForests: null,
forestHosts:{}
};
Resource.setMasterDB = function (dbname) {
console.log('inside setMasterDB', dbname);
this.masterDB = dbname;
};
Resource.getResources = function (dbname) {
var url = '/manage/v2/databases/'+ dbname + '?format=json';
$http.get(url).then(function (response) {
Resource.masterForests = getAttachedForests(response.data);
console.warn('Master Forests = ', Resource.masterForests);
return response;
}).then(function (response) {
Resource.replicaCluster = getReplicaClusters(response.data);
console.warn('Replica Cluster = ',Resource.replicaCluster);
}).then(function () {
console.log('final then', Resource.masterForests);
var reqs = function () {
var arr = [];
angular.forEach(Resource.masterForests, function(forestName){
arr.push($http.get('/manage/v2/forests/'+ forestName + '?format=json'));
});
return arr;
}.call();
console.log('reqs = ', reqs);
$q.all(reqs).then(function (results) {
console.warn(results);
angular.forEach(results, function(result){
console.log('HOST', getForestHost(result.data));
});
return results;
});
});
};
console.warn('RESOURCES: ', JSON.stringify(Resource));
return Resource;
}]);
答案 0 :(得分:0)
我们的情况是必须从两个不同的ajax响应更新数据,我们遵循以下方法。
例如:
function functionname()
{
var First_Response = $http.$get("/test1/test2/...");
var Second_Response = $http.$get("test3/test4/...");
return $q.all([First_Response,Second_Response]); // This function will return only when all the ajax responses are obtained.
}
在以下网址中: https://docs.angularjs.org/api/ng/service/ $ Q
提到$ q.all只有在数组中提到的所有请求都获得ajax响应时才会返回结果。 我们尝试了这种方法,它对我们有用。希望它会给出一些指示
答案 1 :(得分:0)
这是工作代码
getResources: function (dbname) {
var url = '/manage/v2/databases/'+ dbname + '?format=json';
return $http.get(url).then(function (response) {
Resource.masterForests = getAttachedForests(response.data);
Resource.appservers = getAttachedAppServers(response.data);
Resource.replicaClusters = getReplicaClusters(response.data);
console.warn('Master Forests = ', Resource.masterForests);
return Resource.replicaClusters;
}).then(function(replicaClusters) {
var clusterArr = [];
angular.forEach(replicaClusters, function(cluster) {
clusterArr.push($http.get('/manage/v2/clusters/'+ cluster + '?format=json'));
});
return $q.all(clusterArr).then(function(results) {
angular.forEach(results, function(result) {
var cluster = result.data['foreign-cluster-default'].name;
var dbs = getReplicaDBs(result.data);
Resource.replicaDBs[cluster] = dbs;
});
});
}).then(function() {
var forestarr = [];
angular.forEach(Resource.masterForests, function(forestName){
forestarr.push($http.get('/manage/v2/forests/'+ forestName + '?format=json'));
});
return $q.all(forestarr).then(function (results) {
angular.forEach(results, function(result){
var host = getForestHost(result.data);
var forest = result.data['forest-default'].name;
Resource.forestHosts.push(host);
// group forest by hosts
groupForestsByHost(Resource, host, forest);
});
});
});
}
在控制器中
Resource.getResources($scope.db).then(function() {
$scope.masterForests = Resource.masterForests;
$scope.replicaClusters = Resource.replicaClusters;
$scope.forestHosts = Resource.forestHosts;
$scope.forestsOnHosts = Resource.forestsOnHosts;
$scope.replicaDBs = Resource.replicaDBs;
$scope.appservers = Resource.appservers;
}