Ember.js:观察所有对象属性 [英] Ember.js: Observing all object properties

查看:97
本文介绍了Ember.js:观察所有对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想观察对象属性的所有更改。

在以下示例中,我希望通过personChanged观察者通知firstname或lastname是否更改。

但是我想在所有对象属性上使用通用的东西(使用Ember.keys()?)
如何用更通用的东西替换'firstname','lastname'?



在我的例子中:



personChanged 名字或 lastname 已更改:

  App.MyObject:Ember.Object.create({
firstname:'first',
lastname:'last',
personChanged:Ember.observer(function(){
console
$ b $ / $ $ $ $ $ $ $ $ $ $ $ $ b

解决方案

你有什么是使用内联观察器,但你只是有一些语法错误。但是,关键词观察更为简单。所以我已经调整了你的例子。

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

注意:我在属性



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


I would like to observe all changes on an object properties.
In the following example, i would like to be notified by the personChanged observer if firstname or lastname is changed.
BUT I would like to have something generic applied on all object properties (using Ember.keys() ??) How to replace 'firstname', 'lastname' with something more generic ?

In my example:

personChanged is called when firstname or lastname is changed:

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

解决方案

What you have there is right using inline observers, but you just have some syntax errors. However it is even simpler to do with the key word observes. So I have tweaked your example a little below.

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

Note: I put quotes around the properties

Source: http://emberjs.com/guides/object-model/observers/

这篇关于Ember.js:观察所有对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆