Accessing an Ember Object Attribute
I am wondering if there was a change in the way the Ember.Objects were created. Given the code below, I would not expect full_name to return "Joe Smith" as I am not using the access.get () "property, but I recently discovered that it worked and returns" Joe Smith "When is it safe to use simple dot notation and when should you use ".get ('property')"?
note - this code works with ember 1.7
var Person = Ember.Object.extend({
first_name: '',
last_name: '',
full_name: function() {
return this.first_name + ' ' + this.last_name;
}.property()
});
var joe = Person.create({first_name: 'Joe', last_name: 'Smith'});
console.log(joe.get('full_name'));
source to share
In fact, getting / setting the property right after the object worked with pre-cleaning. The place where it hurts you is when you acquire a property that is a computed property or a proxy resource, or you set and it is observed (by nothing, including the dom).
Computed property
Computed properties are stored in the meta object and must be evaluated to provide a value.
var Person = Ember.Object.extend({
first_name: '',
last_name: '',
full_name: function() {
return this.first_name + ' ' + this.last_name;
}.property('first_name', 'last_name')
});
var joe = Person.create({first_name: 'Joe', last_name: 'Smith'});
console.log(joe.full_name); // undefined
Example: http://emberjs.jsbin.com/kaxil/edit?html,css,js,console,output
Proxy server property
This is really obvious in ember controllers and data. The get / set call ObjectController
will not try to get / set the property on the model, rather than mimic the decorator pattern by itself. Similar to Ember Data, where it proxies receives / sets a chain of possible object locations (dirty, in-transit, passed data).
var Person = Ember.Object.extend({
first_name: '',
last_name: '',
full_name: function() {
return this.first_name + ' ' + this.last_name;
}.property('first_name', 'last_name')
});
var joe = Person.create({first_name: 'Joe', last_name: 'Smith'});
var c = Ember.ObjectController.create({
model: joe
});
console.log(c.first_name); // undefined
console.log(c.get('first_name')); // Joe
Example: http://emberjs.jsbin.com/bebavi/edit?html,css,js,console,output
Setter malfunction (respected)
So, if you know that a property exists right on the object, and it is not proxied to another object, there is no harm in just being used obj.property
to get it. Now, if you change a property obj.property = 'foo';
, there is much more potential harm. Because Ember uses an observer pattern that fires on use .set(...)
, you run the risk of changing the dependent property, which will not notify when dependent properties change.
var Person = Ember.Object.extend({
first_name: '',
last_name: '',
full_name: function() {
return this.first_name + ' ' + this.last_name;
}.property('first_name', 'last_name')
});
var joe = Person.create({first_name: 'Joe', last_name: 'Smith'});
console.log(joe.get('full_name'));
joe.first_name = 'foo';
console.log("should be foo smith:" + joe.get('full_name')); // should be foo Smith
joe.set('first_name','asdf');
console.log(joe.get('full_name'));
An example of displaying a set not updating: http://emberjs.jsbin.com/cagufe/edit
Generally, you'll always be safer to use getters / setters, although I'm admittedly lazy in the few situations where I know I don't need to use a getter.
source to share