I'm trying to write a factory that gets data from two sources and uses one source to expand the data in the other object.
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;
});
});
},
}
}]);
Both matches.get() and players.get() are simple GETrequests to an API like so:
app.factory('players', function($http) {
return {
get: function() {
return $http({
method: 'GET',
url: '/players',
});
},
}
});
But the above code (of course) returns the players object while I want it to return the matches object after it gets combined with the players object.
Any tips on how to do this?