Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 在主干视图中侦听嵌套的主干关系模型事件_Backbone.js_Backbone Relational - Fatal编程技术网

Backbone.js 在主干视图中侦听嵌套的主干关系模型事件

Backbone.js 在主干视图中侦听嵌套的主干关系模型事件,backbone.js,backbone-relational,Backbone.js,Backbone Relational,假设我有一个表示群组的主干关系模型-它有一个相关的人集合,每个人都是模型类型的人: CrowdModel = Backbone.RelationalModel.extend({ relations: [{ type: Backbone.HasMany, key: 'People', relatedModel: 'PersonModel', collectionType: 'Backbone.Collection'

假设我有一个表示群组的主干关系模型-它有一个相关的人集合,每个人都是模型类型的人:

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'
    }]
});
每个人都有一个相关的孩子集合,一个孩子也是模范类型的人:

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'
    }]
});
现在,在我看来,我想听听任何一个模型上的变化事件——我们称之为“饥饿”

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!");
    }
});
如果有人饿了,这会被炒鱿鱼,但我正在寻找一种方法,也能听到任何一个孩子的饥饿事件。有什么办法可以把这件事搞得沸沸扬扬的吗

请点击此处:


对不起,这是一个人为的例子——这确实与表示复选框树的模型有关,但这样描述更容易。

你应该从这个人那里冒泡事件

在CrowdView init函数中为ChildHunging添加处理程序

CrowdView = Backbone.View.extend({
          ....
  initialize: function () {
    this.listenTo(this.model.get("People"), 'childHungry', function(){
      console.log("Someone's child is hungry");
    });// listening to child hungry
    this.listenTo(this.model.get("People"), 'change:Hungry', this.hungryChange);
  },
});
人模应该倾听孩子们的声音,并引发孩子们的饥饿感

PersonModel = Backbone.RelationalModel.extend({
      .... 
  initialize: function(){
    this.listenTo(this.get("Children"), 'change:Hungry', this.childHungry);
  },
  childHungry: function(){
    this.trigger("childHungry");
  }

顺便说一句:如果你不想让观众区分饥饿的儿童和饥饿的人,你也可以在上面的ChildHunguing功能中触发change:Hunguing,并保持你的CrowdView版本(请参见)

。谢谢我以前使用过global events.trigger,但我甚至没有意识到可以从模型中触发自定义事件,所以也感谢您让我了解这一点!