Ember.js指南.observes()同步示例不起作用

时间:2015-02-13 23:47:04

标签: javascript ember.js observer-pattern observers

我正在通过Ember Guides工作,reading about observers。他们注意到了

  

Ember的观察者目前是同步的。这意味着一旦他们观察到的一个属性发生变化,它们就会立即触发。

举个例子

Person.reopen({
  lastNameChanged: function() {
    // The observer depends on lastName and so does fullName. Because observers
    // are synchronous, when this function is called the value of fullName is
    // not updated yet so this will log the old value of fullName
    console.log(this.get('fullName'));
  }.observes('lastName')
});

然而,当我try out this code时,它会返回 new ' fullName'。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

您正在观察者中调用 this.get(' fullName'),这会触发 fullName 计算属性以触发并更新该值。

来自Ember doc Whenever you access the fullName property, this function gets called, and it returns the value of the function, which simply calls firstName + lastName.

相关问题