最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone model.create not calling any callback - Stack Overflow

programmeradmin2浏览0评论

I have the following code to create a new model to a collection. The underlying datastore is a remote API:

        var postCreationStatus = this.model.create(newPostModel, {
            wait : true     // waits for server to respond with 200 before adding newly created model to collection
        }, {
            success : function(resp){
                console.log('success callback');
                console.log(resp);
            },
            error : function(err) {
                console.log('error callback');
                console.log(err);
            }
        });

The new model gets created, and I can confirm this from the database, but neither the success nor the error callbacks get called.

After the creation has been pleted, I want to redirect the user. Redirecting prematurely kills the AJAX request, which is why it is important I use the success callback.

The server responds with a JSON response { id : 11 } and an HTTP status of 200 OK.

I have the following code to create a new model to a collection. The underlying datastore is a remote API:

        var postCreationStatus = this.model.create(newPostModel, {
            wait : true     // waits for server to respond with 200 before adding newly created model to collection
        }, {
            success : function(resp){
                console.log('success callback');
                console.log(resp);
            },
            error : function(err) {
                console.log('error callback');
                console.log(err);
            }
        });

The new model gets created, and I can confirm this from the database, but neither the success nor the error callbacks get called.

After the creation has been pleted, I want to redirect the user. Redirecting prematurely kills the AJAX request, which is why it is important I use the success callback.

The server responds with a JSON response { id : 11 } and an HTTP status of 200 OK.

Share Improve this question edited Nov 21, 2017 at 22:37 Ayush asked Nov 28, 2012 at 6:57 AyushAyush 42.4k51 gold badges168 silver badges241 bronze badges 1
  • 1 this title is incorrect, this question is not about collection.create – Alexander Mills Commented Mar 14, 2015 at 4:30
Add a ment  | 

1 Answer 1

Reset to default 6

Looking into the backbone code, I realized my call to the create() function was incorrect. The success and error callbacks needed to be within the object being passed in as the second argument, and not as a third argument. The changed, and working snippet is this:

var postCreationStatus = this.model.create(newPostModel, {
    wait : true,    // waits for server to respond with 200 before adding newly created model to collection

    success : function(resp){
        console.log('success callback');
        console.log(resp);
        that.redirectHomePage();
    },
    error : function(err) {
        console.log('error callback');
        // this error message for dev only
        alert('There was an error. See console for details');
        console.log(err);
    }
});
发布评论

评论列表(0)

  1. 暂无评论