我想观察对象属性的所有变化。
在以下示例中,如果更改了名字或姓氏,我想由personChanged观察者通知。
但是我想在所有对象属性上应用一些通用的东西(使用Ember.keys()?)
如何用更通用的东西替换“名字”,“姓氏”?

在我的示例中:

更改personChangedfirstname时会调用lastname:

 App.MyObject: Ember.Object.create({
   firstname: 'first',
   lastname: 'last',
   personChanged: Ember.observer(function() {
            console.log("person changed");
   }, 'firstname', 'lastname')
 })

最佳答案

使用内联观察器,您所拥有的是正确的,但是您仅遇到一些语法错误。但是,使用关键字observes甚至更简单。因此,我在下面对您的示例进行了一些调整。

App.MyObject: Ember.Object.create({
    firstname: 'first',
    lastname: 'last',
    personChanged: function() {
        //firstname or lastname changed
    }.observes('firstname','lastname')
});

注意:我在属性周围加了引号

来源: http://emberjs.com/guides/object-model/observers/

关于Ember.js:观察所有对象属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14077439/

10-12 17:36
查看更多