1

我有一个引用另一个对象的对象“观察者”:“爸爸”。每次“爸爸”发生变化时,都应该通知“观察者”。

它在爸爸的孩子被改变时有效,但在爸爸的孙子被改变时不起作用。在后一种情况下,我无法让观察者触发。我猜嵌套@each 可能是错误的,但我不知道该怎么做。

当爸爸或其后代中的某些内容被修改时,有没有办法简单地向观察者发送通用通知?

App.Dad = DS.Model.extend({
    sons: DS.hasMany('App.Son'),
    name: null
})

App.Son = DS.Model.extend({
    dad: DS.belongsTo('App.Dad'),
    sons: DS.hasMany('App.GrandSon'),
    name: null
})

App.GrandSon = DS.Model.extend({
    dad: DS.belongsTo('App.Son'),
    name: null
})

App.Watcher = Ember.Object.extend({
    dad: null, // will be set later
    dadChanged: function(){
        // Fires if a son is changed, but not
        // if a grand son is changed
    }.observes('dad.sons.@each.name','dad.sons.@each.sons.@each.name')
})

参见说明这一点的 jsFiddle:http: //jsfiddle.net/4kUzr/1/

4

1 回答 1

0

您没有详细概述您的用例,但从对象建模的角度来看,我怀疑以这种方式工作:

App.Person = DS.Model.extend({
    dad: DS.belongsTo('App.Person'),
    sons: DS.hasMany('App.Person'),
    name: null,
    nameObserver : function(){
       var dad = this.get("dad");
       // do some logic
       // hand it afterwards to the dad of this person
       if(dad)
         dad.nameObserver()
    }.observes("name")
});

所以只有1 个类而不是多个类,我建立了父子关系

于 2013-03-13T15:41:24.343 回答