我正在尝试编写一个从两个源获取数据的工厂,并使用一个源来扩展另一个对象中的数据.
app.factory('information', ['$http', '$q', 'players', 'matches', function($http, $q, players, matches) {
return {
// Returns all matches and players including extra parsing
get: function() {
return players.get().success(function(data) {
players = data;
matches.get().success(function(data) {
matches = data;
for ( match in matches ) {
matches[match].a_player1 = players.filter(function(player) { return player.username == matches[match].a_player1 })[0];
matches[match].a_player2 = players.filter(function(player) { return player.username == matches[match].a_player2 })[0];
matches[match].b_player1 = players.filter(function(player) { return player.username == matches[match].b_player1 })[0];
matches[match].b_player2 = players.filter(function(player) { return player.username == matches[match].b_player2 })[0];
console.log(matches)
}
return matches;
});
});
},
}
}]);
Run Code Online (Sandbox Code Playgroud)
这两个matches.get()和players.get()简单GET的请求,像这样的API:
app.factory('players', function($http) {
return {
get: function() {
return $http({
method: 'GET',
url: '/players',
});
},
}
});
Run Code Online (Sandbox Code Playgroud)
但是上面的代码(当然)返回players对象,而我希望它在与matches对象结合后返回players对象.
关于如何做到这一点的任何提示?
该函数不会返回任何内容,因为您无法直接从异步操作返回值,无论是返回promise还是使用回调.但我认为你在寻找的是$q.all:
return {
// Returns all matches and players including extra parsing
getEverything: function() {
var getPlayers= $http({
method: 'GET',
url: '/players',
});
var getMatches= $http({
method: 'GET',
url: '/matches',
});
return $q.all([getPlayers,getMatches]);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
getEverything().then(function(data){
var players=data[0].data;
var matches=data[1].data;
})
Run Code Online (Sandbox Code Playgroud)
编辑:
不相关,但要在工厂搬回来:
getEverything: function() {
var getPlayers= $http({
method: 'GET',
url: '/players',
});
var getMatches= $http({
method: 'GET',
url: '/matches',
});
return $q.all([getPlayers,getMatches]);
},
getEverythingMapped:function(){
var deferred = $q.defer();
this.getEverything().then(function(data){
var players=data[0].data;
var matches=data[1].data;
//do as many loops on either result set as you like
for ( match in matches ) {
matches[match].a_player1 = players.filter(function(player) { return player.username == matches[match].a_player1 })[0];
matches[match].a_player2 = players.filter(function(player) { return player.username == matches[match].a_player2 })[0];
matches[match].b_player1 = players.filter(function(player) { return player.username == matches[match].b_player1 })[0];
matches[match].b_player2 = players.filter(function(player) { return player.username == matches[match].b_player2 })[0];
console.log(matches)
//use of a promise here allows us top use this method using -then, we need to so this since we're
//waiting for the result of an async server call before we can loop through players and matches
deferred.resolve(matches);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您将在控制器中使用上述方法:
information.getEverythingMapped().then(function(matches){
console.log(matches);
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4561 次 |
| 最近记录: |