【问题标题】:angularjs ngResource update a list of entities and then update another oneangularjs ngResource 更新实体列表,然后更新另一个
【发布时间】:2015-03-23 07:46:07
【问题描述】:

我有 2 个资源:项目及其任务。 当用户需要更新项目时,他们可能会更新标题,也可以更新其任务。

如果用户创建一个任务,需要先将它保存到数据库中才能取回id,然后我可以将id分配给项目的tasks属性。

因此需要在保存所有任务后保存项目。

    $scope.update = function() {

        //each taskTemplate can be updated concurrently, or at the same time. they do not need to updated one by one
        for (var taskTemplates in $scope.taskTemplates) {
            //i need to update all the tasks, and then I can update the project
        }

        //this needs to be update after all the taskTemplates are updated
        $scope.projectTemplate.$update(function() {
            $location.path('projectTemplate/' + $scope.projectTemplate._id)
        }, function(errResponse) {
            $scope.error = errorResponse.data.message
        })

    }

如何实现这样的排序?

【问题讨论】:

    标签: angularjs node.js ngresource


    【解决方案1】:

    将任务承诺与all() 组合并使用all(...).then(...) 保存项目。详情:

    $scope.update = function() {
        var promises = [];
        for (var taskTemplates in $scope.taskTemplates) {
            promises.push(taskResource.save(taskTemplates).$promise);
        }
        $q.all(promises).then(function() {
            return projectResource.save(currentProject).$promise;
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      相关资源
      最近更新 更多