链接承诺,以延迟执行对角应用循环 [英] Chaining promise and defered with a for loop in angular application

查看:180
本文介绍了链接承诺,以延迟执行对角应用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用承诺,延迟执行的角度应用程序。

我用两个函数 $ scope.checkQuestions() $ scope.saveExerciseApi()
但问题是,之前首先完成至极不是我预期的第二个启动。

第一个调用承诺更新每个问题(用REST服务)
然后更新它在页面上的形式使用 $ scope.formQuestion [] 阵列。

$ scope.Questions $ scope.Exercises 已经承诺对象更新,并在REST创建实体服务。

$ scope.formQuestion $ scope.formexercise $ scope.exercises 是在网页上使用的数据。

  $ scope.checkQuestions =功能(){
递延= $ q.defer()
为(在$ scope.formQuestion我){
    如果($ scope.formQuestion [I]的.pk == NULL){//保存
        $ scope.Questions.add($ scope.formQuestion [I]),然后(功能(newquestion){
            deferred.resolve(newquestion);
            $ scope.formQuestion [I] = newquestion;
            });
        }
    }
返回deferred.promise;
}$ scope.saveExerciseApi =功能(){
    如果($ scope.formexercise.pk == NULL){//保存
        $ scope.Exercises.add($ scope.formexercise)。然后(功能(newexercise){
            $ scope.exercises.push(newexercise);
            $ scope.showform = FALSE;
            });
        }
}$ scope.saveExercise =功能(){
    $ scope.checkQuestions(),然后($ scope.saveExerciseApi)。
}

编辑:

下面的更改不会解决问题。

1) $ scope.formexercise.questions 仍是空的,

2)$ scope.saveExerciseApi推出从未发生

这有什么错在下面的逻辑?有没有遍历一个更美好,更清洁的方式更新提问本地数据和后更新运动数据?

  $ scope.checkQuestions =功能(){
    递延= $ q.defer()
    $ scope.formexercise.questions =阵列();
    为(在$ scope.formQuestion我){
            如果($ scope.formQuestion [I]的.pk == NULL){
            $ scope.Questions.add($ scope.formQuestion [I]),然后(功能(newquestion){
                $ scope.formQuestion [I] = newquestion;
                $ scope.formexercise.questions.push($ scope.formQuestion [I]的.pk);
                如果($ scope.formQuestion.length == I + 1){
                deferred.resolve(newquestion);
                }
            });
          }其他{
            $ scope.Questions.edit($ scope.formQuestion [I]的.pk,$ scope.formQuestion [I]),然后(功能(updatequestion){
            $ scope.formexercise.questions.push($ scope.formQuestion [I]的.pk);
            如果($ scope.formQuestion.length == I + 1){
               deferred.resolve(udpatequestion);
            }
         });
        }
       }
返回deferred.promise;
}


解决方案

您解决方法被调用,一旦第一个问题被保存。 Resolve方法应该将所有的新的问题,我相信以后被称为

  $ scope.checkQuestions =功能(){
递延= $ q.defer()
变种questionsToSave = 0;
为(在$ scope.formQuestion我){
    如果($ scope.formQuestion [I]的.pk == NULL){//保存
        INT questionsToSave = questionsToSave + 1;
        $ scope.Questions.add($ scope.formQuestion [I]),然后(功能(newquestion){
            $ scope.formQuestion [I] = newquestion;
            如果(questionsToSave&下; = 1)
               deferred.resolve(newquestion);
            其他
               questionsToSave = questionsToSave-1;
            });
        }
    }
返回deferred.promise;
}

I try to use promise and defered in an angular application.

I use two functions $scope.checkQuestions() and $scope.saveExerciseApi() but the problem is that the second one is launched before the first has finished wich is not what I had expected.

The first one call promises to update each questions (with a REST service) and then update $scope.formQuestion[] array which is used in a form on the page.

$scope.Questions and $scope.Exercises are already promise object to update and create entities in a REST service.

$scope.formQuestion, $scope.formexercise, $scope.exercises are data used on the webpage.

$scope.checkQuestions = function(){
deferred = $q.defer()
for (i in $scope.formQuestion){
    if ($scope.formQuestion[i].pk == null){     //Save
        $scope.Questions.add($scope.formQuestion[i]).then(function(newquestion){
            deferred.resolve(newquestion);
            $scope.formQuestion[i] = newquestion;
            });
        }    
    }
return deferred.promise;
}

$scope.saveExerciseApi = function() {
    if ($scope.formexercise.pk == null){        //Save
        $scope.Exercises.add($scope.formexercise).then(function(newexercise){
            $scope.exercises.push(newexercise);
            $scope.showform = false;
            });
        }
}

$scope.saveExercise = function() {
    $scope.checkQuestions().then($scope.saveExerciseApi);
}

EDIT:

The following change doesn't fix the problem.

1) $scope.formexercise.questions is still empty and

2) $scope.saveExerciseApi launch never occured

Is there anything wrong in the following logic ? Is there a better and cleaner way to loop over and update local data for questions and after to update exercise data ?

$scope.checkQuestions = function(){
    deferred = $q.defer()
    $scope.formexercise.questions = Array ();
    for (i in $scope.formQuestion){                 
            if ($scope.formQuestion[i].pk == null){
            $scope.Questions.add($scope.formQuestion[i]).then(function(newquestion){
                $scope.formQuestion[i] = newquestion;
                $scope.formexercise.questions.push($scope.formQuestion[i].pk);
                if ($scope.formQuestion.length == i+1){
                deferred.resolve(newquestion);
                }
            });
          } else {
            $scope.Questions.edit($scope.formQuestion[i].pk, $scope.formQuestion[i]).then(function(updatequestion){
            $scope.formexercise.questions.push($scope.formQuestion[i].pk);
            if ($scope.formQuestion.length == i+1){
               deferred.resolve(udpatequestion);
            }
         });
        }
       }
return deferred.promise;
}

解决方案

You resolve method is called as soon first question is saved. The resolve method should be called after adding all new question i believe

$scope.checkQuestions = function(){
deferred = $q.defer()
var questionsToSave=0;
for (i in $scope.formQuestion){
    if ($scope.formQuestion[i].pk == null){     //Save
        int questionsToSave=questionsToSave + 1;
        $scope.Questions.add($scope.formQuestion[i]).then(function(newquestion){
            $scope.formQuestion[i] = newquestion;
            if(questionsToSave<=1)
               deferred.resolve(newquestion);
            else
               questionsToSave=questionsToSave-1;
            });
        }    
    }
return deferred.promise;
}

这篇关于链接承诺,以延迟执行对角应用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆