如何从angularjs-rails-resource获取create()、update()上的确认或回调

How to get a confirmation or callback from angularjs-rails-resource on create(), update()

本文关键字:确认 回调 update 获取 angularjs-rails-resource create      更新时间:2023-09-26

我正在使用AngularJs Rails资源,并且想知道当您使用create()update()方法成功创建/更新资源时是否有回调。

例如,下面是我的代码:
$scope.createPage = function () {
    var page = new Page({
        title: $scope.pageTitle,
        content: $scope.pageContent,
        published: true
    });
    if (page.create()){
        $scope.showAlert('Page created successfully', 'success');
    } else {
        $scope.showAlert('There was a problem creating the page.', 'warning');
    }
}

但这不起作用。即使API不可用,也会创建成功消息。

任何想法?

你应该使用Using resources:

$scope.createPage = function () {
    var page = new Page({
        title: $scope.pageTitle,
        content: $scope.pageContent,
        published: true
    });
    page.create().then(function (results) {
        console.log(results);
        $scope.showAlert('Page created successfully', 'success');
    }, function (error) {
        console.log(error);
        $scope.showAlert('There was a problem creating the page.', 'warning');
    });
}