Skip to content

Instantly share code, notes, and snippets.

@scottsword
Created October 21, 2016 15:44
Show Gist options
  • Save scottsword/1183c35a7e44d6e92eead9082b7b49f2 to your computer and use it in GitHub Desktop.
Save scottsword/1183c35a7e44d6e92eead9082b7b49f2 to your computer and use it in GitHub Desktop.
Little underscore-based schedule service.
(function() {
'use strict';
function scheduleFactory($log, $q, events) {
/*
* NOTE: This method organizes the list of event data
* into a nested list for schedule iteration.
* Days > Times > Events
* */
return {
_serialize: function _serialize(fortName) {
var fortEvents = events.cache
.filter(function (event) {
if (event.EventType === fortName) {
return events;
}
});
return _.chain(fortEvents)
.groupBy(function (event) {
return event.EventStart.split(" ")[0];
})
.map(function (events, day) {
return {day: day, events: events};
})
.sortBy(function (event) {
return event.day.split("-")[2];
})
.map(function (day) {
day.times = _.chain(day.events)
.groupBy(function (event) {
return event.EventStart.split(" ")[1];
})
.map(function (events, time) {
//return {time: time, events: events};
return {time: {label:time, fulldate: events[0].EventStart}, events: events};
})
.sortBy(function (time) {
return time.time.label.split(":")[0];
})
.value();
delete day.events;
return day;
})
.value();
},
build: function build(fortName) {
var dfd = $q.defer();
var self = this;
events.getAll()
.then(function() {
dfd.resolve(self._serialize(fortName));
})
.catch(function (err) {
$log.warn(err);
dfd.reject(err);
});
return dfd.promise;
}
};
}
angular.module('TMF15')
.factory('schedule', scheduleFactory);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment