Reputation: 155
Backbone collection 0.9.9 - add event not work After update backbone to 0.9.9, i have a problem with (add).
(function () {
var Model = Backbone.Model.extend({
initialize: function () {
console.log('initialize Model');
}
});
var Collection = Backbone.Collection.extend({
model: Model,
url: "/json.json",
initialize: function () {
console.log('initialize Collection');
}
});
var View = Backbone.View.extend({
initialize: function () {
console.log('initialize View');
}
});
var collection = new Collection([
{
"id" : "001",
"name" : "Дарья",
"text" : "1 Вопрос - Ответ 1"
}
]);
collection.on('add', function () {
console.log('edd event', this)
});
collection.fetch({
add: true,
//silent: false,
success: function (model) {
console.log('fetch')
}
});
}());
console.log('edd event', this) - not work ( in old versions it works
Upvotes: 2
Views: 1305
Reputation: 18556
From what I gather from checking out the Backbone 0.9.9 source, the add
-option does nothing with fetch
unless you add in the update
- option as well. source
So to do something useful with it do the following:
collection.fetch({
add: true,
update: true, // this is necessary as well
//silent: false,
success: function (model) {
console.log('fetch')
}
});
This is also the cause for your problem. When you fetch, the Collection
automatically defaults to the reset
-function after fetching. reset
silences the add
-events and opts to just trigger a reset
event as is evident from the Backbone.js source
if (models) this.add(models, _.extend({silent: true}, options));
So use the update
option if you want the add
-events and do not want to empty the collection before adding the new models. If you must have the reset functionality AND the add
event, then you might have to write some custom implementation of reset
.
Upvotes: 1
Reputation: 35890
It would appear that the add
option to collection.fetch
is no longer supported.
From 0.9.2 source (collection.fetch):
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
From 0.9.9 source (collection.fetch):
var method = options.update ? 'update' : 'reset';
collection[method](resp, options);
if (success) success(collection, resp, options);
So by default collection.fetch
will cause a reset on the collection, and the reset
event will be fired. If you pass option update:true
, and update
will be performed.
According to documentation for the new collection.update method, update will trigger the add
events for added models, so the following should work:
collection.fetch({
update: true,
success: function (model) {
console.log('fetch')
}
});
Just test and be aware, that the new update method will also trigger remove
and change
events, if models are removed or changed.
Upvotes: 3
Reputation: 3318
You need to remove the commented out line because it won't propagate the add events otherwise (see line 827 of the backbone.js source). So the following should work
collection.fetch({
add: true,
silent: false,
success: function (model) {
console.log('fetch')
}
});
I'm not sure if that's a change from previous versions :)
Upvotes: 1