Angular $资源和使用Interceptor来检查响应/错误/回调

时间:2013-09-02 01:35:41

标签: angularjs angular-resource

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

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

//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”对象值由仅具有project_id且其他字段被替换的服务器返回的新响应替换。

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

2 个答案:

答案 0 :(得分: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;
                });
            }

        };

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

如果您的回调与$save$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;
            });

        };

希望有所帮助!

答案 1 :(得分: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);

                }
            };