【问题标题】:Ember.js Guide's .observes() sync example not workingEmber.js 指南的 .observes() 同步示例不起作用
【发布时间】:2015-04-15 03:33:50
【问题描述】:

我正在阅读 Ember 指南,我是 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'。

为什么会这样?

【问题讨论】:

    标签: javascript ember.js observer-pattern observers


    【解决方案1】:

    您正在观察者中调用 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.

    【讨论】:

    • 如果这是真的,为什么指南会说这将“记录 fullName 的旧值”?如果他们的示例始终是最新的,那么什么不是?
    猜你喜欢
    • 1970-01-01
    • 2013-10-21
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多