Backbone:传递或检索集合更新的详细信息

Backbone: pass or retrieve details on a Collection update

我认为这让我的问题得到了理解。

var coll = myBackboneCollection();
coll.on('update', this.collUpdated, this);
coll.create({ id: 1 });

collUpdated: function (collection, options) {
    var newId = ???;
}

想知道回调函数中newId为1。这可能吗?如果可能的话,我不想在 Collection 上使用状态变量。

据我在 docs 中看到的,create 只会直接触发以下内容:

Creating a model will cause an immediate "add" event to be triggered on the collection, a "request" event as the new model is sent to the server, as well as a "sync" event, once the server has responded with the successful creation of the model

据此判断,您应该正在收听 add 事件。

还要注意create方法的签名是collection.create(attributes, [options]).

因此,如果您尝试设置模型的实际 id 属性,您应该将其作为第二个参数传递给选项:

var coll = myBackboneCollection();
coll.on('add', this.collUpdated, this);
coll.create({},{ id: 1 });
collUpdated: function (model, collection, options) {
  if(model.id === 1){
  }
}