I have a controller calling to any set of specified methods in a factory to retrieve information on a given date. Some of the methods iterate through a JSON file to return promise. See factory below with only two methods fleshed out.
emmanuel.factory('DayService', function($http, $q){
var obj = {};
obj.dayInWeek = function(d){
// receives a mm/dd/yyyy string which then it converts it to day object to parse the weekday value
var date = new Date(d);
var weekday = new Array(
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday'
);
return weekday[date.getDay()];
}
obj.season = function(d){
// receives a mm/dd/yyyy string parses against Calendar service for liturgical season
var day = new Date(d).getTime();
//$window.alert(day);
var promise = $q.defer();
$http.get('content/calendar.json').success(function(data) {
//$window.alert(data.calendar.seasons.season.length);
for (var i=0; i<data.calendar.seasons.season.length; i++){
var start = new Date(data.calendar.seasons.season[i].start);
var end = new Date(data.calendar.seasons.season[i].end);
end.setHours(23,59);
//$window.alert(start +'-'+ end +' || '+ day);
if (day >= start && day <= end){
//$window.alert(data.calendar.seasons.season[i].name);
var temp = data.calendar.seasons.season[i].name;
promise.resolve(temp);
//$window.alert(promise);
break;
}
}
});
return promise.promise;
}
obj.weekInSeason = function(d){
/*...
return promise.promise;
*/
}
obj.holiday = function(d){
/*...
return promise.promise;
*/
}
return obj;
});
I was tried to write multiple temp.then(function(var) {...}); for each of the methods to store the values in a $scope.date model but to my dismay only the last .then instance would store the value.
emmanuel.controller('WeekdayMorning', function($scope, DayService){
var tempWeek = DayService.weekInSeason(today);
var tempSeason = DayService.season(today);
tempWeek.then(function(week) {
$scope.date = {weekNum: DayService.weekInSeason(today)};
$scope.date = {oscWeek: week % 2};
});
tempSeason.then(function(season) {
$scope.date = {season: DayService.season(today)};
});
});
How can I retrieve the data from the factory promise either individual methods or as a complete $scope.date model?