【问题标题】:Backbone model.create not calling any callback骨干model.create不调用任何回调
【发布时间】:2012-11-28 06:57:05
【问题描述】:

我有以下代码来为集合创建一个新模型。底层数据存储是一个远程 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);
            }
        });

新模型被创建,我可以从数据库中确认这一点,但成功和错误回调都没有被调用。

创建完成后,我想重定向用户。重定向过早地终止了 AJAX 请求,这就是为什么我使用成功回调很重要。

服务器以 JSON 响应 { id : 11 } 和 HTTP 状态 200 OK 进行响应。

【问题讨论】:

  • 这个标题不正确,这个问题与collection.create无关

标签: javascript backbone.js backbone-model


【解决方案1】:

查看主干代码,我意识到我对create() 函数的调用不正确。成功和错误回调需要在作为第二个参数传入的对象中,而不是作为第三个参数。改变后的工作 sn-p 是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多