gpt4 book ai didi

backbone.js - 从集合中删除模型时如何获取模型的索引 - Backbone

转载 作者:行者123 更新时间:2023-12-04 04:58:22 27 4
gpt4 key购买 nike

从集合中删除模型时,如何获取模型的索引。
在下面的代码中有一个回调函数 doSomething(){} ,它在 remove 被触发时被调用,我希望索引在那里。

Backbone 文档说“移除前模型的索引可作为 options.index 提供给听众”,
但我得到的选项是未定义的。

MyModel = Backbone.Model.extend({});    

var MyCollection = Backbone.Collection.extend({
model : MyModel
});

var data = [

{
"id": 1,
"value": 600
},
{
"id": 2,
"value": 800
},
{
"id": 3,
"value": 700
},
{
"id": 7,
"value": 100
}

];

var newCollection = new MyCollection();
newCollection.on('remove',doSomething);
newCollection.reset(data);

function doSomething(){/* how to get the index of the deleted model */}
newCollection.remove(newCollection.at(2));

console.log(newCollection);

fiddle - http://jsfiddle.net/Z7Qmg/

最佳答案

function doSomething(model, collection, options) {
console.log(options.index);
}

http://jsfiddle.net/vpetrychuk/Z7Qmg/1/

关于backbone.js - 从集合中删除模型时如何获取模型的索引 - Backbone ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16458948/

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