2014-06-09 26 views
0

对象1和对象2都是对象(数组)的一部分,并且每个对象都具有someAttribute:DS.attr( '串');我想调用对象的特定实例,例如{{object.someAttribute 2}}

所以数组是仅有的两个在这个例子中的对象,我想的someAttribut

值我知道如何通过每个对象循环使用{{#each}} 但我想叫一个对象,不是它们全部的列表

我打电话{{ object.someAttribute 2 }},在那里我声明对象,我想显示的属性,然后它的ID?

我想知道如何调用此对象及其属性任何页面。

感谢您的帮助!

+1

是否是对象数组?或者是范围应该是一个数组,我不知道我们从哪里获得第三个对象。 – Kingpin2k

+0

感谢您的输入!我已经更新了这个问题要更加清楚。我希望如此帮助。 –

回答

1

要呼叫的阵列的特定项/对象在车把,使用

{{objects.[1].someAttribute}}访问所述阵列中的第二对象的someAttribute属性。

例如, 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"}) 
    ]; 
    } 
}); 

还检查了这个How do I access an access array item by index in handlebars?

+0

很好回答,melc。 +1 –

相关问题