问题描述:

I am working through the Ember Guides and am reading about observers. They note that

Observers in Ember are currently synchronous. This means that they will fire as soon as one of the properties they observe changes.

And give the example that

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')

});

Yet when I try out this code it returns the new 'fullName'.

Why is this happening?

网友答案:

You are calling this.get('fullName') in your observer, which triggers the fullName computed property to fire and update the value.

From 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.

相关阅读:
Top