gpt4 book ai didi

javascript - 从本地文件加载数据时,Backbone 会触发 'change' 事件吗?

转载 作者:搜寻专家 更新时间:2023-11-01 05:15:06 25 4
gpt4 key购买 nike

我正在尝试使用以下代码测试主干集合的更改事件:

var Item = Backbone.Model.extend({});
var ItemCollection = Backbone.Collection.extend({
model: Item,
url: "data.json"
});
var collection = new ItemCollection();
collection.bind("change", function() {cosole.log("collection changed.");});
collection.fetch();

然后我手动更改json文件并再次调用collection.fetch(),没有发生'change'事件,是因为我使用本地json文件还是.fetch方法无法触发'change'事件?

最佳答案

因为获取集合调用了重置方法,所以触发了一个重置事件。

fetch collection.fetch([options])
.... When the model data returns from the server, the collection will reset...

reset collection.reset(models, [options])
... Use reset to replace a collection with a new list of models (or attribute hashes), triggering a single "reset" event at the end....

如果您在 fetch 方法中指定选项 { add: true },模型将添加到集合中而不是替换它,因此会触发 add 事件。


change 事件在模型更改时触发,所以基本上是在模型上调用方法 .set() 时触发。

关于javascript - 从本地文件加载数据时,Backbone 会触发 'change' 事件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9479336/

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