TemplateService.js

angular.module('homepage').
// Store templates, location and widget data
factory('TemplateService', function (AddTemplates, DateUtility, GlobalDataService, Server, Structs, TimelineService) {
	var DateRange = new Structs.DateRange();

	function setDateRange(date) {
		if(DateRange.start === null || DateRange.end  === null) {
			DateRange.start = new Date(date);
			DateRange.end   = new Date(date);
			return;
		}
		if(date.getTime() < DateRange.start.getTime()) {
			DateRange.start = new Date(date);
		}
		else if(date.getTime() > DateRange.end.getTime()) {
			DateRange.end   = new Date(date);
		}
	}

	function getTemplates() {
		var date = GlobalDataService.getSelectedDate();
		var isWithinDateRange = (DateRange.start !== null && DateRange.end !== null) ?
			DateUtility.isBetweenDates(date, DateRange.start, DateRange.end, true, true) : false;

		if(!isWithinDateRange) {
			// Set DateRange
			setDateRange(date);

			TimelineService.setLoadingStatus(true);
			// Send server current date timestamp (GMT) and timezone offset
			Server.getTemplates({
					'startTimestamp': DateUtility.getDateOnly(date).getTime(),
					'endTimestamp': DateUtility.getDateEnd(date).getTime()
				})
				.success(function (result) {
					console.log(result);
					AddTemplates.addData(result.data.templates);
					TimelineService.setLoadingStatus(false);
				});
		}
	}

	return {
		getTemplates: getTemplates
	};
}).
/**
 * This is to keep functionality between user and demo data dashboard in sync
 * @type {[type]}
 */
factory('AddTemplates', function (DataStoreSrvc) {
	return {
		addData: function (templates) {
			// Add templates to data store
			for(var index = 0; index < templates.length; index++) {
				var template = templates[index];
				DataStoreSrvc.add(template);
			}
		}
	};
});