在AngularJS资源节约承诺行动 [英] Promise on AngularJS resource save action
问题描述
我目前工作的一个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
但奇怪的是,如果我一样,那么回调添加到发现者之一(例如findById)一切工作正常。但返回值回归当然$保存()是不确定的,相比返回值返回course.get({courseId:ID});这是Object对象。
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,一个用于与资源实例工作,并 - 在缺乏更好的话 - 更宽泛的版本。主要的区别beeing使用 $
的 - prefixed方法( GET
VS $获得
)
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
)
的 $
- prefixed在<一个方法href=\"https://github.com/angular/angular.js/blob/v1.2.0rc1/src/ngResource/resource.js#L523\">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); });
通过实例化资源 $
- prefixed方法:
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屋!