JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://cloud.github.com/downloads/emberjs/ember.js/ember-0.9.7.1.min.js"></script>
<script type="text/x-handlebars">
<div>person1: firstName={{MyApp.person1.firstName}}, lastName= {{MyApp.person1.lastName}}, fullName={{MyApp.person1.fullName}}</div>
<div>person2: firstName={{MyApp.person2.firstName}}, lastName= {{MyApp.person2.lastName}}, fullName={{MyApp.person2.fullName}}</div>
<div>person3: firstName={{MyApp.person3.firstName}}, lastName= {{MyApp.person3.lastName}}, fullName={{MyApp.person3.fullName}}</div>
</script>
JavaScript
// One of the downsides to the behavior demonstrated @ http://jsfiddle.net/zJQJw/2/
// is the following does not work as expected:
MyApp = Ember.Application.create();
Person = Ember.Object.extend({
init: function() {
if(!this.get('lastName')) this.set('lastName', 'Doe'); // we've got a John Doe, ensure there is always a last name
},
firstName: null,
lastName: null,
fullName: Ember.computed(function(key, value) {
if (arguments.length === 1) { // getter
var firstName = this.get('firstName');
var lastName = this.get('lastName');
return firstName + ' ' + lastName;
} else { // setter
var name = value.split(" ");
this.set('firstName', name[0]);
this.set('lastName', name[1]); // this intentionally not defaulted to "Doe" to compare with the init() behavior
return value;
}
}).property('firstName', 'lastName')
});
// Case 1: init() runs, but the fullName setter has been overwritten so firstName is broken.
MyApp.person1 = Person.create({fullName: 'Tyrion'});
// Case 2: init() runs before we've initialized the object
MyApp.person2 = Person.create().setProperties({fullName: 'Robb'});
// Case 3: you need to know that you can't create() with fullName:
MyApp.person3 = Person.create({firstName: 'Robb'});