对 AngularJS 资源保存操作的承诺 [英] Promise on AngularJS resource save action

查看:20
本文介绍了对 AngularJS 资源保存操作的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发 REST + AngularJS 应用程序.

I am currently working on a REST + AngularJS application.

关于资源节约行动的承诺,我有一个小问题.

I have a little problem concerning promises on resource save action.

我的工厂:

App.factory('Course', function($resource) {
    var course = $resource('/AppServer/admin/courses/:courseId', {}, {});

    course.findAll = function() {
        return course.query();
    };

    course.findById = function(id) {
        return course.get({
            courseId : id
        });

    };

    course.saveCourse = function(course) {
        return course.$save();
    }

    return course;
});

我的控制器:

App.controller('CourseEditController', function($scope, $routeParams, $location, Course, FlashMessage) {
    // load course into edit form
    $scope.course = Course.findById($routeParams.courseId);

    // save edited course and print flash message
    $scope.saveCourse = function() {
        var savedCourse = Course.saveCourse($scope.course);

        savedCourse.$then(function(httpResponse) {
            FlashMessage.set("Die Änderungen am Kurs <i>" + savedCourse.title + "</i> wurden erfolgreich gespeichert.");
            $location.path("/kurse/verwalten");
        });
    }
}); 

现在的问题是,我收到以下异常:

Now the problem is, that I get the following exception:

TypeError: Cannot call method '$then' of undefined

奇怪的是,如果我将相同的 then-callback 添加到其中一个查找器(例如 findById),则一切正常.但是与return course.get({courseId:id});"的返回值相比,return course.$save()"的返回值是未定义的这是对象对象".

The strange thing is that If I add the same then-callback to one of the finders (e.g. findById) everything works fine. But the return value of "return course.$save()" is undefined, compared to the return value of "return course.get({courseId:id});" which is "Object object".

我想要的是在完全执行保存操作时而不是在此之前设置 FlashMessage.

What I want is to set the FlashMessage when the save action was fully executed and not before that.

对此有什么想法吗?我的 REST 服务的响应是正确的.它返回保存的对象.

Any ideas on this? The response from my REST service is correct. It returns the saved object.

问候马克

推荐答案

有两种稍微不同的 API,一种用于处理资源实例,另一种是更通用的版本.主要区别在于使用 $ 前缀方法(get$get)

There is two slightly different API's, one for working with a resource instance and - in lack of better words - more generic version. The main difference beeing the use of $-prefixed methods (get vs $get)

ngResource/resource.js.代理调用并直接返回承诺.

The $-prefixed methods in ngResource/resource.js. proxies the call and returns the promise directly.

AFAIK 在资源被实例化之前,您只能使用普通的 get 访问资源.

AFAIK before the resource gets instanciated, you can only access resources with the normal get.

var promise = Resource.get().$promise;

promise.then(function(res)  { console.log("success: ", res); });
promise.catch(function(res) { console.log("error: ", res); });

使用实例化资源,可以使用 $ 前缀的方法:

With instanciated resource the $-prefixed methods are available:

var res = new Resource({foo: "bar"});

res.$save()
    .then(function(res)  { console.log("authenticated") })
    .catch(function(req) { console.log("error saving obj"); })
    .finally(function()  { console.log("always called") });

这篇关于对 AngularJS 资源保存操作的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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