【发布时间】:2013-12-05 11:41:50
【问题描述】:
假设我有一个代表人群的 Backbone Relational 模型 - 它有一个相关的 People 集合,每个模型类型都是 Person:
CrowdModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'People',
relatedModel: 'PersonModel',
collectionType: 'Backbone.Collection'
}]
});
每个人都有一个相关的孩子集合,一个孩子也是模型类型的人:
PersonModel = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasMany,
key: 'Children',
relatedModel: 'PersonModel',
collectionType: 'Backbone.Collection'
}]
});
现在,在我看来,我想在任何模型上收听更改事件 - 我们称之为“Hungry”。
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!");
}
});
如果任何人饿了,这将触发,但我正在寻找一种方法来监听任何孩子的饥饿事件。有什么办法可以把它冒泡吗?
jsfiddle 在这里:http://jsfiddle.net/fnj58/1/
抱歉,这个人为的例子 - 这确实与表示复选框树的模型有关,但这种方式更容易描述。
【问题讨论】:
标签: backbone.js backbone-relational