Say I have a Backbone Relational model representing a crowd - it has a related collection of People, each of model type Person:
CrowdModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'People',
relatedModel: 'PersonModel',
collectionType: 'Backbone.Collection'
}]
});
Each Person has a related collection of Children, a child also being of model type Person:
PersonModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'Children',
relatedModel: 'PersonModel',
collectionType: 'Backbone.Collection'
}]
});
Now, in my view, I'd like to listen to a change event - let's call it "Hungry" - on any of the models.
CrowdView = Backbone.View.extend({
initialize: function () {
this.listenTo(this.model.get("People"), 'change:Hungry', this.hungryChange);
},
hungryChange: function(model, val, options) {
if(val) console.log("Somebody's hungry!");
}
});
This will fire if any of the People are hungry, but I'm looking to find a way in the view to also listen for the hungry event on any of the Children. Is there some way of bubbling this up?
jsfiddle here: http://jsfiddle.net/fnj58/1/
Sorry for the contrived example - this really has to do with a model representing a tree of checkboxes, but it's easier to describe this way.