ember.js - Dynamic computed alias -
i'm trying update component value dynamically controller, without explicitly passing value through template. following hard-coded solution works:
foo: em.computed.oneway('bar') however, i'm looking have this:
slab: 'bar', foo: em.computed.oneway(slab) its important values bound together, , not initial value when component starts up. there way this?
update january 2015: coincidentally, wrote small addon few days ago in response issue in ember github. this addon allows have dynamic computed properties. solution:
slab: 'bar', foo: ember.computed.indirect('slab') that keep 2 bound together, foo property point @ whichever property slab value refers to.
maybe (taking example):
slab: function() { return mysterystring; }.property(), foo: undefined, _slabobserver: null, _slabobserverkey: null, _slabdidchange: function() { // remove old observer if (this.get('_slabobserverkey')) { this.removeobserver(this, this.get('_slabobserverkey'), this.get('_slabobserver')); } // create new 1 this.set('_slabobserver', function() { // update foo when value key held 'slab' changes this.set('foo', this.get(this.get('slab'))); }); // watch property key held 'slab' , call method created this.addobserver(this, this.get('slab'), this.get('_slabobserver')); // hold last key can remove observer after changes this.set('_slabobserverkey', this.get('slab')); }.property('slab') to walk through it:
the controller created ,
slabundefined. causesslabchange, triggering_slabdidchangeobserver. (note useproperty, notobserveson_slabdidchangebecause former cause function called soon. source)_slabdidchangefires. skips removing old observer because haven't created 1 yet. adds new observer watches property heldslabproperty. stores key , observer can remove them later.fooupdated whenever property observed changes. in example, anytimebarupdates,fooupdates.if
slabchanges, step 2 repeats, removes old observer first, not have conflicting updatesfoo.
the idea have add observer bar property manually. haven't tested this, idea should clear.
Comments
Post a Comment