2

最近几天,我正在努力调用 REST 服务并跟踪响应、错误、回调等。我已经阅读了大部分帖子,但是由于我对 Angular 的理解有限,我似乎无法理解它。以下是我到目前为止的问题和理解。

我正在使用仅返回“project_id”的 Project.$update() 服务。此服务器不再返回完整数据。以下是在这里分享的几行代码。

//create Project factory
app.factory('Project', function ($resource) {
    return $resource('/api/projects/:projectid',
            {projectid:'@id'},
            {update: {method:'PUT', isArray:false}}    
    );

});

以下是我用来更新/创建项目的指令中的代码。

//save project
            scope.saveProject = function (project) {
                //update modified by field
                project.modifiedby = scope.user._id;
                //change to view mode
                scope.projectView = 1;
                //call server to save the data                
                if (project._id == undefined || project._id == "") {

                    //Call server to create new and update projectID
                    project._id = project.$save()._id;
                }
                else {
                    //Call server to update the project data
                    project.$update({ projectid: project._id });
                }


            };

以下是 save() 和 update() 的服务响应。

{"_id":"52223481e4b0c4d1a050c25e"}

这里的问题是;“项目”对象值被服务器返回的新响应替换,只有 project_id 和其他字段被替换。

我正在阅读有关 $resource 的详细文档,但我无法掌握它。在这里获得一些指导来编写代码来检测错误、响应、回调会很棒。

4

2 回答 2

0

您可以将原始对象替换为成功回调中从服务器返回的对象,如下所示:

        //save project
        scope.saveProject = function (project) {

            //update modified by field
            project.modifiedby = scope.user._id;

            //change to view mode
            scope.projectView = 1;

            //call server to save the data                
            if (project._id == undefined || project._id == "") {

                //Call server to create new and update projectID
                project.$save(function(updatedProject, headers){

                    // Replace project by project returned by server
                    project = updatedProject;
                });
            }
            else {
                //Call server to update the project data
                project.$update(function(updatedProject, headers){

                    // Replace project by project returned by server
                    project = updatedProject;
                });
            }

        };

一旦收到服务器响应,这将用服务器返回的对象替换原始对象。

如果您的回调与$saveand$update方法相同,您可以进一步简化代码,如下所示:

        //save project
        scope.saveProject = function (project) {

            //update modified by field
            project.modifiedby = scope.user._id;

            //change to view mode
            scope.projectView = 1;

            var action = (angular.isDefined(project._id)) ? '$update' : '$save';

            //call server to save the data                
            project[action](function(updatedProject, headers){

                // Replace project by project returned by server
                project = updatedProject;
            });

        };

希望有帮助!

于 2013-09-02T20:55:20.967 回答
0

根据 jvandemo 和 BoxerBucks 的建议;我通过传递带有原始数据副本的回调方法来使用以下方法进行保存/更新。但是,我仍在寻找处理错误/成功状态的中央方法。请建议。

//save project metadta
            scope.saveProjectMetadta = function (project) {

                //update modified by field
                project.modifiedby = scope.user._id;
                //change to view mode
                scope.projectView = 1;
                //keep original data to pass into callback
                var originalProjectObject = angular.copy(project);
                //call server to save the data                
                if (project._id == undefined || project._id == "") {                    
                    //Call server to create new and update projectID
                    project.$save(originalProjectObject, function (projectResponse) {
                        originalProjectObject._id = projectResponse._id;
                        //update scope
                        scope.project = originalProjectObject;
                        //invoke method to update controller project object state
                        scope.updateProjectScope(scope.project);
                    });

                }
                else {
                        //Call server to update the project data
                        project.$update({ projectid: project._id }, function (projectResponse) {

                        originalProjectObject._id = projectResponse._id;
                        //update scope
                        scope.project = originalProjectObject;
                        //invoke method to update controller project object state
                        scope.updateProjectScope(scope.project);

                    },originalProjectObject);

                }
            };
于 2013-09-04T22:11:45.503 回答