ember.js - Ember Object attribute access -
i wondering if there alter in way ember.objects created. given code below, not expect full_name homecoming "joe smith" not using ".get()" property accessor, found worked , homecoming "joe smith". when safe utilize simple dot notation , when should utilize ".get('property')"?
note -this code works ember 1.7
var person = ember.object.extend({ first_name: '', last_name: '', full_name: function() { homecoming this.first_name + ' ' + this.last_name; }.property() }); var joe = person.create({first_name: 'joe', last_name: 'smith'}); console.log(joe.get('full_name'));
actually getting/setting property right off of object has worked since pre-ember. place hurts when property getting computed property or proxy property or you're setting , it's beingness observed (by anything, including dom).
computed propertycomputed properties stored in meta of object, , need evaluated in order provide value.
var person = ember.object.extend({ first_name: '', last_name: '', full_name: function() { homecoming 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 propertythis apparent in controllers , ember data. calling get/set on objectcontroller
effort get/set property on model instead of on mimicking decorator pattern. ember info proxies gets/sets downwards chain of possible locations of property (dirty, in transit, committed data).
var person = ember.object.extend({ first_name: '', last_name: '', full_name: function() { homecoming 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 problem (being observed)so, beingness said, if know property exists right on object , isn't beingness proxied downwards object there no harm in using obj.property
getting. if changing property obj.property = 'foo';
there lot more possible harm. since ember uses observer pattern triggered through utilize of .set(...)
run risk of changing dependent property won't notify dependent properties it's changed.
var person = ember.object.extend({ first_name: '', last_name: '', full_name: function() { homecoming 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 foo smith:" + joe.get('full_name')); // should foo smith joe.set('first_name','asdf'); console.log(joe.get('full_name'));
example showing set not updating: http://emberjs.jsbin.com/cagufe/edit
you safer using getters/setters, though i'm admittedly lazy in few situations know don't need utilize getter.
ember.js
No comments:
Post a Comment