Skip to content

Instantly share code, notes, and snippets.

@THEtheChad
Created October 19, 2012 15:06
Show Gist options
  • Save THEtheChad/3918720 to your computer and use it in GitHub Desktop.
Save THEtheChad/3918720 to your computer and use it in GitHub Desktop.
Async event builder for Gatorade Showcase
build_event: function buildEvent(id, callback){
var mungeData
, responses = []
, path = '/events/' + id
, workers = [
'',
'/stats',
'/roster',
'/votes',
'/attendees'
]
;//var
//
// Takes the responses in the response array and
// munges them together. This function is only executed
// after it is called X number of times (X being the
// number of workers) to ensure that all responses
// have been recieved
//
mungeData = _.after(workers.length, function(){
//
// Check all the responses for errors
//
if(_.any(responses, hasError)) return;
function hasError(res){
var data = res.response;
if(res.status != 200 || res.status != 201 || || !data || data.error || data == "Invalid signed request"){
showError("An error occured getting your event.");
app.router.navigate('dashboard', {'trigger': true});
return true;
}
return false;
}
//
// Responses are stored in an array
// in the order they were called
//
var root = responses[0].response
, stats = responses[1].response
, rosters = responses[2].response
, votes = responses[3].response
, attendees = responses[4].response
;//var
//
// Combine the responses and pass them back
// to the callback
//
callback(stitchParts(app.user.id, root, stats, rosters, votes, attendees));
});
//
// Make ajax calls in parallel for each of the
// worker endpoints
//
_.each(workers, function(endpoint, idx){
api.get(path + endpoint + '.json', {'signed_req':app.signed_req}, collector(idx))
});
//
// Collector is used to gather responses in the
// order they were requested
// It also executes mungedata after each response is
// returned
//
function collector(id){ return function(res){ responses[id] = res; mungeData() } }
function stitchParts(fbUserId, root, stats, rosters, votes, attendees) {
root.mvp_pick = votes.my_mvp;
root.team_pick = votes.my_team;
var teams = {}
teams[root.team.id] = root.team;
teams[root.opponent_team.id] = root.opponent_team;
/* this would easily extend to mulitple teams */
for (var teamId in teams) {
var team = teams[teamId]
, roster = rosters[teamId] || {};
team.num_votes = votes.teams[teamId] || 0;
team.attendees = attendees[teamId] || [];
team.roster = roster.players || [];
team.supporters = roster.supporters || [];
team.supporter_count = roster.supporters.length;
team.user_role = null;
/* is the user a player on this team? */
for (var n = team.roster.length; n --; )
if (fbUserId === team.roster[n].fb_user_id)
team.user_role = 'player';
/* is the user a supporter of this team? */
for (var n = team.supporters.length; n --; )
if (fbUserId === team.supporters[n].fb_user_id)
team.user_role = 'supporter';
delete team.supporters;
for (var n = team.roster.length; n --; ) {
var player = team.roster[n];
player.id = player.team_membership_id;
player.mvp_votes = votes.players[player.id] || 0;
player.stats = stats[player.id] || {};
player.stat_source = player.stats.stat_source;
delete player.stats.stat_source;
}
}
return root;
}
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment