AngularJS - 从工厂数据解析控制器中的多个.then

时间:2014-03-12 06:05:46

标签: angularjs angularjs-scope angularjs-controller

我有一个控制器调用工厂中的任何指定方法集来检索给定日期的信息。一些方法遍历JSON文件以返回promise。只见两种方法,见下面的工厂。

            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;
            });

我试图写多个temp.then(function(var){...});对于将值存储在$ scope.date模型中的每个方法,但令我沮丧的是,只有最后一个.then实例会存储该值。

            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)};
                });
            });

如何从工厂承诺中检索单个方法或完整的$ scope.date模型中的数据?

3 个答案:

答案 0 :(得分:1)

改为使用$q.allOfficial documentation for $q):

getData: function (today) {
                return $q.all([
                   DayService.weekInSeason(today),
                   DayService.season(today)
                ]);
          };

然后像这样使用then

getData(today).then(function(aggregatedData){
$scope.resultFromWeekInSeason = aggregatedData[0];
$scope.resultFromSeason = aggregatedData[1];
});

请注意,这只会在两个呼叫都成功时解决,同时进行呼叫。

答案 1 :(得分:0)

承诺一旦解决了一个值,我相信不能改变,但你试图在for循环中一次又一次地用不同的值来解决相同的承诺。我不确定你的方案是如何工作的,但你可以使用promise通知机制,你可以在for循环中执行此操作

promise.notify(temp);

并在循环后执行

promise.resolve({});

您可以在

的第三个参数中捕获进度

then(successCallback, errorCallback, notifyCallback)

另一种选择是将temp值整理成for循环内的数组。

然后最后到promise.resolve(allTemps)

答案 2 :(得分:0)

谢谢大家的帮助。我遇到的问题与反复声明$ scope的模型有关,而不是多个promises。

每次重新声明$scope.date= {attr : ...}时,我都删除了所有以前的属性值,因此只留下最后一个声明来包含任何值。向Angular $ scope模型声明多个属性值的正确方法是

$scope.date = { weekday: DayService.dayInWeek(today), season: DayService.season(today), weekNum: week, oscWeek: week % 2, holiday: DayService.holiday(today), nextDay: DayService.nextDay(today) }