2

I'm using AngularJs Rails Resouce and was wondering if there was a callback for when you successfully create/update a resource using the create() or update() methods.

For instance, here's my code:

$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');
    }
}

But that doesn't work. Even if the API is not available, the success message is created.

Any ideas?

1 Answer 1

1

You should use the promise look at 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');
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That was exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.