dimanche 29 mars 2015

How to proxy methods on the prototype using ObjectProxy in ember.js?

I've got a simple ObjectProxy that I'd like to make calling methods on easy(er). For example, the object I plan to "proxy" has a method named foo and bar - they will not be available directly causing me to do something like this.



this.get("model").get("content").foo();
this.get("model").get("content").bar();


Instead I'd much prefer if foo and bar acted like they were on the object



this.get("model").foo();
this.get("model").bar();


I can make this happen (long hand) by hard coding the foo and bar methods on the ObjectProxy itself - then pulling content and invoking the method manually w/ apply like so



return Ember.ObjectProxy.extend({
content: function() {
var filter_value = this.get("filter_value");
return this.get("source").filterBy("id", filter_value).objectAt(0);
}.property("source.[]"),
foo: function() {
var content = Ember.get(this, 'content');
return content["foo"].apply(content, arguments);
},
bar: function() {
var content = Ember.get(this, 'content');
return content["bar"].apply(content, arguments);
}
}).create({
filter_value: id,
source: store.find(type)
});


If instead I wanted to proxy each "method" like this - how can I set the prototype in a way that won't hurt the tree that ember has built up already?





Aucun commentaire:

Enregistrer un commentaire