手柄模板中的href标签中的Ember插值 [英] Ember Interpolation in a href tag in handlebars template

查看:133
本文介绍了手柄模板中的href标签中的Ember插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个动态地址插入href字段的google地图的简单链接。我已经尝试了下面的代码加上吨的其他乱七八糟的没有运气。如何在一个句柄href字段中插入一个动态的ember字符串?

I am trying to make a simple link to google maps with a dynamic address inserted into the href field. I have tried the code below plus tons of other messing around with no luck. How do you interpolate a dynamic ember string in a handlebars href field?

我正在使用ember,rails和handlebars。
我知道如何使用bindAttr如果我的整个URL存储与我的模型,但我只有地址。将每个模型的google网址看起来不必要,如果我可以在视图中调用一次。

I am using ember,rails, and handlebars. I know how to use bindAttr if I had the entire url stored with my model but I only have the address. Putting the google url with every model seemed unnecessary if i could just call it once in the view.

<h1>{{name}}</h1>
 <div>
  <p><a {{bindAttr href='http://maps.com/?1=address'}}>{{address}}</a></p>
 </div>

<h1>{{name}}</h1>
<div>
  <p><a href='http://maps.google.com/?q={{address}}'>{{address}}</a></p>
</div>

我用来修复它

App.Location = DS.Model.extend(
  name: DS.attr('string', defaultValue: "")
  address:  DS.attr('string', defaultValue: "")
  fullAddress: (->
    "http://maps.google.com/?q=#{@get('address')}"
  ).property('address')
)


推荐答案

p>您可以这样做,请参阅演示

You could do something like this, see demo.

基本上你可以创建一个 Mixin 用于常见属性,然后将其混合到您的模型中。
例如:

Basically you could create a Mixin for common properties and then mix it in your models. For example:

App.BaseModel = Ember.Mixin.create({
  base: 'http://maps.google.com/?q=',
  fullAddress: function(){
    return this.get('base') + this.get('address');
  }.property('address')
});

App.MyModel = DS.Model.extend(App.BaseModel, {
  name: DS.attr('string'),
  address: DS.attr('string')
});

所以你可以稍后在你的模板中使用它:

So you could later use it in you templates like this:

{{#each model}}
<h1>{{name}}</h1>
  <div>
    <p><a {{bind-attr href='fullAddress'}}>{{address}}</a></p>
  </div>
{{/each}}

希望它有帮助。

这篇关于手柄模板中的href标签中的Ember插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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