1

Object 1 and Object 2 are all a part of an Objects (array), and each object has someAttribute: DS.attr('string');

So the array is only two objects for this example, and I want to the value of someAttribut

I know how to loop through each Object with {{#each}} but I want one object called, not a list of all of them.

Do I call {{ object.someAttribute 2 }}, where i state the object, the attribute i want displayed and then its ID?

I want to know how to call this object and its attributes on any page.

Thanks for the help!

Ian Steffy
  • 1,234
  • 3
  • 18
  • 45

1 Answers1

2

To call specific item / object of an array in handlebars, use

{{objects.[1].someAttribute}} to access the someAttribute property of the second object in the array.

example, http://emberjs.jsbin.com/xagewugi/1/edit

hbs

<script type="text/x-handlebars" data-template-name="index">
  using <b>\{{each}}</b> helper
    <ul>
    {{#each item in model}}
      <li>id: {{item.id}}<br/>
  someAttr1: {{item.someAttr1}}</li>
    {{/each}}
    </ul>
  calling an object <b>directly</b><br/>
  this is the first object's attributes:<br/>
  id: {{model.[0].id}}<br/>
  someAttr1: {{model.[0].someAttr1}}
  </script>

js

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return [
      Em.Object.create({id:1,someAttr1:"attribute of 1"}),
      Em.Object.create({id:2,someAttr1:"attribute of 2"}),
      Em.Object.create({id:3,someAttr1:"attribute of 3"}),
      Em.Object.create({id:4,someAttr1:"attribute of 4"})
    ];
  }
});

also check out this How do I access an access array item by index in handlebars?

Community
  • 1
  • 1
melc
  • 11,523
  • 3
  • 36
  • 41