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:

  1. the controller created , slab undefined. causes slab change, triggering _slabdidchange observer. (note use property , not observes on _slabdidchange because former cause function called soon. source)

  2. _slabdidchange fires. skips removing old observer because haven't created 1 yet. adds new observer watches property held slab property. stores key , observer can remove them later.

  3. foo updated whenever property observed changes. in example, anytime bar updates, foo updates.

  4. if slab changes, step 2 repeats, removes old observer first, not have conflicting updates foo.

the idea have add observer bar property manually. haven't tested this, idea should clear.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -