Skip to content

Instantly share code, notes, and snippets.

@wycats
Created March 13, 2012 03:56
Show Gist options
  • Save wycats/2026642 to your computer and use it in GitHub Desktop.
Save wycats/2026642 to your computer and use it in GitHub Desktop.
Person = Ember.Object.extend({
fullName: function() {
var gender = this.get('gender'), parts, married;
if (gender === "M") {
parts = ["Mr."];
} else if (gender === "F") {
married = this.get('married');
parts = [married ? "Mrs." : "Ms."];
} else {
parts = []
}
parts.push(this.get('firstName'));
parts.push(this.get('lastName'));
}.property(),
fullNameDidChange: function() {
alert("my full name has changed yo!");
}.observes('fullName')
});
// "prewarming" Person would mean executing the computed property against
// a new object, hoping it was valid (aside: it wouldn't be in many cases,
// but let's pretend). This would result in dependencies of gender, firstName
// and lastName.
// In practice, simply prewarming Person wouldn't work anyway, because:
// the fullName property here depends on gender, firstName and lastName
var yehuda = Person.create({ firstName: "Yehuda", lastName: "Katz" });
// the fullName property here depends on gender, firstName, lastName AND married
var leah = Person.create({ gender: "F", firstName: "Leah", lastName: "Silber", married: false });
// In both cases, note that we have not yet evaluated fullName
leah.set('married', true)
// expected alert, but how can you get it without prewarming when you created?
// Note that computed properties can be arbitrarily expensive, and triggering all
// computed properties when an object was created means triggering computed
// properties that may never be used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment