在handlebars模板中读取对象属性 [英] Read object properties in handlebars template

查看:184
本文介绍了在handlebars模板中读取对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {{#每个对象中的对象) } 
< tr>
{{#each key in keys}}
{{! - 不行,因为object [key]是无效的语法我猜 - }}
< td> {{对象[键]}}< / TD>
{{/ each}}
< / tr>
{{/ each}}

我知道我可以读这样的属性 {{object.someProperty}} ,但是在我的情况下,需要读取的属性列表作为参数传递给组件(在我的例子中,它被称为 key )。



也许这个函数已经存在于句柄或者余烬中,我还没有找到? >

解决方案

该语法对于Ember的Handlebars不起作用。由于优化,Ember希望将数组观察器添加到被迭代的项目中,它将无法正常工作。您可以做的是添加一个计算属性,返回对象中的项目的数组。您确实需要小心,计算的属性将不会随着值的更改而更新。



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

  iterableObject:function(){
return $ .map(this.get('model'),function(value,key){
return [key +' - '+ value];
});
} .property('model')


Is there a some way to read object properties using handlebars or ember helpers?

{{#each object in objects}}
  <tr>
    {{#each key in keys}}
      {{!- doesn't work, because object[key] isn't valid syntax I guess --}}
      <td>{{object[key]}}</td> 
    {{/each}}
  </tr>
{{/each}}

I know that I can read properties like so {{object.someProperty}}, however in my case the list of properties which needs to be read is passed to a component as an argument (in my example it is called keys).

Maybe this function already exists in handlebars or ember and I just didn't find it yet?

解决方案

That syntax doesn't work for Ember's Handlebars. Because of optimization and Ember wanting to add array observers to the item being iterated it won't work. What you can do is add a computed property that returns an array of the items in the object though. You do need to be careful though, the computed property won't update as values change.

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

iterableObject: function(){
  return $.map(this.get('model'), function(value, key) {
    return [key + '-' + value];
  });
}.property('model')

这篇关于在handlebars模板中读取对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆