模型.保存设置url并在成功时进行回调

model.save setting the url and making a call back on success

本文关键字:回调 成功 保存 设置 url 模型      更新时间:2023-09-26

我想让这个方法工作:

that.model.save(null,  // I'm calling that.model because this is nested in another method
{
   url: 'some url',
    success: function(model, response) {
        // update stuff
    }, 
    error: function(model, response) {
        // throw error. 
    }
}); 

但是由于某种原因它没有调用成功方法或错误方法。注释在哪里我在调用方法。我不想讨论我调用的所有方法的细节。save方法也能工作,它只是不调用这些方法中的任何一个。

如果我尝试调用一个我已经创建的方法,比如:

success: that.somemethod()

javascript抛出:Uncaught TypeError: Illegal invocation

任何帮助都将非常感激。

这个方法看起来很好,因为它使用了与Backbone描述的相同的参数:

model.save([attributes], [options])

最可能的情况是,你的成功/错误处理程序在原型链的某个地方被覆盖,而没有调用你的处理程序。

首先,你必须绑定实例方法,然后将其赋值为success

that.model.save(null,  // I'm calling that.model because this is nested in another method
{
    url: 'some url',
    success: _.bind(that.mySuccessMethod, that), 
    error:   _.bind(that.myErrorMethod, that)
});