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 badges2 Answers
Reset to default 9Because 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