简体   繁体   中英

Access ember js object from template

I have an ember controller as follows

App.IndexController = Ember.Controller.extend({
   edwin:{
    uno:'hola',
    dos:'hola dos',
    tres:'hola tres'
  }
});

I my template I need the value of the property edwin, if I get the property as

  <script type="text/x-handlebars" data-template-name="index">
  {{edwin.dos}}
  </script>

It works fine, but I need to select the object element from other variable, when I try

 <script type="text/x-handlebars" data-template-name="index">
  {{edwin['dos']}}
  </script>

or

 <script type="text/x-handlebars" data-template-name="index">
  {{edwin[myvar]}}
  </script>

I can't display the value this is my jsbin

http://emberjs.jsbin.com/xupuse/1/edit

any suggestions

The closest you'll get it creating some dynamic property in the controller, based on another property. The real problem will be if the other property changes, ember won't know to update it. In the example below, if edwin.xxxx changed the computed property wouldn't know*, whereas if dynamo changed it would know (since it's watching it).

App.IndexController = Ember.Controller.extend({
  edwin:{
    uno:'hola',
    dos:'hola dos',
    tres:'hola tres'
  },
  dynamo: 'uno',
  dynamoProperty: function(){
    return this.get('edwin.' + this.get('dynamo'));
  }.property('dynamo')
});

http://emberjs.jsbin.com/beziq/1/edit

*I'm kind of lying here, you could specify each and every property on edwin and it would always be marked dirty, regardless of whether or not it applied to dynamoProperty a la

  dynamoProperty: function(){
    return this.get('edwin.' + this.get('dynamo'));
  }.property('dynamo', 'edwin.{uno,dos,tres}')

http://emberjs.jsbin.com/beziq/2/edit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM