0

试图获取已保存模型的 id

    var newBookmark = bookmarks.create(values,{
        success:function(){
            console.log('Successfuly saved')
            console.log(idOfThisModel) //need id from the server here
            this.close()
        },
        error: function(){console.log('error, didnt save')},
    })

如何将模型的 id 放入成功方法中?

4

2 回答 2

1

您需要将其作为函数参数传递:

success:function( idOfThisModel ){
于 2012-12-14T15:46:52.737 回答
1

来自 Backbone.jsfetch 文档

接受选项哈希中的成功和错误回调,它们分别作为参数传递 (model, response, options) 和 (model, xhr, options)。

所以你只需要使用传递给你的回调的参数

success: function(model, resp, opts) {
  var id = model[whateverYourIdAttributeIs];
  // ... etc
}
于 2012-12-14T15:47:23.653 回答