最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does Backbone trigger a 'change' event when load data from a local file? - Stack Overflow

programmeradmin0浏览0评论

I'm trying to test the change event of backbone collection, using this code:

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();

then I change the json file manually and call collection.fetch() again, no 'change' event happens, is it because I use a local json file or .fetch method cannot trigger 'change' event?

I'm trying to test the change event of backbone collection, using this code:

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();

then I change the json file manually and call collection.fetch() again, no 'change' event happens, is it because I use a local json file or .fetch method cannot trigger 'change' event?

Share Improve this question edited Feb 28, 2012 at 9:27 dencey asked Feb 28, 2012 at 9:25 denceydencey 1,06117 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Because fetching a collection calls the reset method, a reset event is fired.

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....

If you specify the option { add: true } in the fetch method, models are added to the collection instead of replacing it, so a add event is fired.


The change event is triggered when a model changes, so basically when the method .set() is called on a model.

'change' event gets trigger when one of the collection attribute changes. Eventhough you changed the file on your own, I couldn't find any file attribute in the ItemCollection .The two attribute you are having is a model object and a string.So I guess that's why the 'change' event is not getting triggered

发布评论

评论列表(0)

  1. 暂无评论