Skip to content

Instantly share code, notes, and snippets.

@crofty
Created February 8, 2012 12:12
Show Gist options
  • Save crofty/1768615 to your computer and use it in GitHub Desktop.
Save crofty/1768615 to your computer and use it in GitHub Desktop.
Example of observers in Ember
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
@gma
Copy link

gma commented Feb 8, 2012

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment