For my app, there are matches and schedules. My idea is simple, but i cannot find a way to do it with angularjs for its asyn nature.
for matches, there are so called [a, b, c] three matches as an example. for schedules, there are more than three resources such as [1, 2, 4, 5, 6, 7].
I would like to pair matches with available schedules so that [a1, b2, c4] with unique pairing.
I just wonder why the follow code doesn't work:
for (var u in matches){
var matchDefer = $q.defer();
var matchPromise = matchDefer.promise;
var match = matches[u]
var schedule = schedules[u];
Matches.get({matchId: match._id}, function(matchResponse){
console.info('schedule', schedule);
matchResponse.AddSchedule(schedule, matchDefer);
});
matchPromise.then(function(result){
console.info('result', result);
}, function(reason){
console.info('reason', reason);
});
}
And the following code is in the match service:
Matches.prototype.AddSchedule = function(schedule, defer) {
this.schedule = schedule._id;
this.$update(function(matchResponse){
schedule.match = matchResponse._id;
schedule.$update(function(scheduleResponse){
defer.resolve(scheduleResponse._id);
});
});
Could anyone help me even just give me some hints to do it, thanks!