Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js: Raise model change event after fetching the collection?

Tags:

backbone.js

I have a model with a name attribute that has a bind function to be called when the attribute is changed:

var Workitem = Backbone.Model.extend({
                                 initialize: function(){
                                 this.bind('change:name', function() {
                                           alert('name');
                                           }
                                           });
                                 },
                                 defaults: {
                                 name: 'Name'
                                 }
                                 });

If I fetch a single model, this works, and the change event is triggered when I set the attribute and when I fetch the data.

But then I've created a collection and I want the event to be triggered for each fetched model into the collection during the fetch method:

var WorkitemList = Backbone.Collection.extend({
                                       model: Workitem,
                                       });

var workitemsList = new WorkitemList();
workitemsList.fetch();

Is this possible?

like image 437
RaTiO Avatar asked Dec 05 '25 10:12

RaTiO


1 Answers

Fetch triggers 'reset' event on the collection when import is done. listen on it, fire / mess with models then.

like image 131
ddotsenko Avatar answered Dec 08 '25 11:12

ddotsenko