获取嵌套对象时在hbs中获得帮助 [英] Get helper in hbs when getting nested object

查看:89
本文介绍了获取嵌套对象时在hbs中获得帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下对象:

image: {
  size: {
    l: {
      url: 'l.jpg',
    },
    m: {
      url: 'm.jpg',
    },
    s; {
      url: 's.jpg',
    }
  }
},
mySize: 'm'

如果我想在模板中获取相应的图片网址,该怎么办?我尝试过的

If I want to get corresponding image url in my template, how should I do that? I tried:

{{get image mySize 'url'}}

,但它不起作用。

我可以通过键入获取所需的网址像这样:

I can get the url I want by typing like this:

{{get (get image mySize) 'url')}}

但是,这是一个非常不直观且丑陋的解决方法。有没有更好的办法?谢谢。

However this is a very unintuitive and ugly workaround. Is there any a better way? Thank you.

推荐答案

您需要同时使用concat帮助器:

You need to use the concat helper along with it:

{{获取图像(concat'size。'mySize'.url')}}

但这听起来像是计算属性的工作:

But this sounds like a job for a computed property:

imageUrl: Ember.computed('mySize', 'image.size', function() {
  let { image, mySize } = this.getProperties('image', 'mySize');
  return Ember.get(image, `size.${mySize}.url`);
})

这样,您可以只使用 {模板中的{imageUrl}}

灰烬微动

这篇关于获取嵌套对象时在hbs中获得帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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