Created
February 8, 2012 12:12
-
-
Save crofty/1768615 to your computer and use it in GitHub Desktop.
Example of observers in Ember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App = Ember.Application.create() | |
Person = Ember.Object.extend({ | |
fullName: Ember.computed(function() { | |
var firstName = this.get('firstName'); | |
var lastName = this.get('lastName'); | |
return firstName + ' ' + lastName; | |
}).property('firstName', 'lastName') | |
}); | |
App.person = Person.create({ | |
firstName: "Yehuda", | |
lastName: "Katz" | |
}); | |
App.otherPerson = Person.create({ | |
firstName: "James", | |
lastName: "Croft", | |
iAmObserving: function(){ | |
// This method will be triggered when App.person firstName changes | |
alert('the name was changed') | |
}.observes('App.person.firstName') | |
}); | |
App.person.set('firstName', "Brohuda"); // observer will fire |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to look at that for a minute or two before I spotted line 23, which is a slightly different use to the one in the docs. The mental leap that I'd failed to make was that you could define your callback on the object that is doing the observing and specify a property relative to App, rather than on the object that contains the observed property.
Cheers for taking the time to write it up.