One of my biggest annoyances with ember is .get
, .set
central. I understand there is some sort of plan to move to ECMAScript proxies later on to resolve this.
At Discourse we don’t need to support IE8 so I was thinking why not just meta program the properties so we can avoid the need for .get and .set , getters and setters are everywhere we really care about http://kangax.github.io/es5-compat-table/
The trivial, inefficient solution is:
window.Foo = Ember.Object.extend({
init: function(){
var me, prop, i;
this._super();
me = this;
if (this.properties && this.properties.length) {
for(i=0; i<this.properties.length; i++) {
prop = this.properties[i];
Object.defineProperty(me, prop, {
set: function(val){
Ember.propertyWillChange(me, prop, val);
this["__" + prop] = val;
Ember.propertyDidChange(me, prop, val);
},
get: function(){ return this.get("__" + prop); }
});
}
}
}
});
window.Bar = Foo.extend({
properties: ["baz"],
yay: function(){
console.log("baz: " + this.baz);
}.observes("baz")
});
var b = Bar.create();
b.baz = 10;
// "baz: 10"
Even this very rough cut is not way worse that the current state: http://jsperf.com/ember-auto-prop once the getter / setter is bolted to the prototype it will be way faster.
Additionally, computed properties need some extra magic, but it all feels feasible.
Has anyone thought about this problem / solution, should this be a switch in Ember ?