gpt4 book ai didi

javascript - Backbone : pass or retrieve details on a Collection update

转载 作者:行者123 更新时间:2023-12-03 08:16:20 26 4
gpt4 key购买 nike

我认为这可以解答我的问题。

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

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

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

最佳答案

从我在 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){
}
}

关于javascript - Backbone : pass or retrieve details on a Collection update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33924497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com