Created
August 23, 2014 15:19
-
-
Save alastairparagas/25b798ed7a199b311c0b to your computer and use it in GitHub Desktop.
Example of Q all Promise Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (window) { | |
'use strict'; | |
var angular = window.angular; | |
angular.module('myhonorsCitizenship').factory('CitizenshipService', ['FirebaseIO', 'UserService', 'FirebaseCollection', '$q', 'EventService', function (FirebaseIO, UserService, FirebaseCollection, $q, EventService) { | |
var citizenshipFactory = { | |
getTypes: function () { | |
var defer = $q.defer(); | |
FirebaseIO.child('system_settings/eventTypes').once('value', function (snapshot) { | |
var citizenshipTypes = {}, | |
citizenshipTypesEnabled = {}, | |
citizenshipTypesDisabled = {}; | |
angular.forEach(snapshot.val(), function (value, key) { | |
if (value.citizenship == undefined) { | |
return; | |
} | |
var typeIsEnabled = value.citizenship.enabled == undefined ? true : value.citizenship.enabled, | |
typePoints = value.citizenship.points || 0, | |
typeMaxPoints = value.citizenship.maxPoints || 0; | |
citizenshipTypes[key] = { | |
enabled: typeIsEnabled, | |
points: typePoints, | |
maxPoints: typeMaxPoints | |
}; | |
if (typeIsEnabled) { | |
citizenshipTypesEnabled[key] = { | |
points: typePoints, | |
maxPoints: typeMaxPoints | |
}; | |
} else { | |
citizenshipTypesDisabled[key] = { | |
points: typePoints, | |
maxPoints: typeMaxPoints | |
}; | |
} | |
}); | |
defer.resolve({ | |
types: citizenshipTypes, | |
enabledTypes: citizenshipTypesEnabled, | |
disabledTypes: citizenshipTypesDisabled | |
}); | |
}); | |
return defer.promise; | |
}, | |
getUser: function (pantherId) { | |
if (pantherId == undefined) { | |
pantherId = UserService.profile.id; | |
} | |
var defer = $q.defer(); | |
citizenshipFactory.getTypes().then(function (typesPromise) { | |
FirebaseIO.child('user_profiles/' + pantherId + '/attendance').once('value', function (snapshot) { | |
var individualEventsDefer = [], | |
userEvents = {}, | |
eventTypes = typesPromise.enabledTypes, | |
citizenshipPoints = 0; | |
angular.forEach(snapshot.val(), function (eventDetails, eventId) { | |
var eventType = eventDetails.eventType[0], | |
individualEventDefer = $q.defer(); | |
if (!eventTypes[eventType]) { | |
return; | |
} | |
if (userEvents[eventType] === undefined) { | |
userEvents[eventType] = []; | |
} | |
FirebaseIO.child('events/' + eventId + "/name").once('value', function (snapshot) { | |
userEvents[eventType].push(snapshot.val()); | |
individualEventDefer.resolve(); | |
}); | |
individualEventsDefer.push(individualEventDefer.promise); | |
}); | |
$q.all(individualEventsDefer).then(function () { | |
angular.forEach(userEvents, function (eventId, eventType) { | |
var pointsForEventType = userEvents[eventType].length * eventTypes[eventType].points, | |
maxPointsForEventType = eventTypes[eventType].maxPoints; | |
citizenshipPoints += (maxPointsForEventType !== 0 && pointsForEventType > maxPointsForEventType) ? maxPointsForEventType : pointsForEventType; | |
}); | |
defer.resolve({ points: citizenshipPoints, events: userEvents }); | |
}); | |
}); | |
}); | |
return defer.promise; | |
} | |
}; | |
return citizenshipFactory; | |
}]); | |
}(window)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment