I have a Model with an image_id
property. I have a View for that Model that contains an image element. I need to insert the id into the image element's src
property to complete the URL of the image so that I'm effectively doing this:
<img src="news + newsItem.imageID + .jpg"></img>
My first attempt used a Handlebars helper:
<img src="news{{newsImageURL newsItem.imageID}}.jpg"></img>
But this also inserted Metamprph script tags around it:
<img url="<script id='metamorph-10-start' type='text/x-placeholder'></script>news/7.jpg<script id='metamorph-10-end' type='text/x-placeholder'></script>" alt="" class="content-image news-media" /></a>
So I've looked at using bindAttr
, however as I need to transform the image_id value(by prepending and appending the rest of the path), this doesn't seem like a solution either.
There are 2 possibilities:
1- Declare the image path as computed property on your model. If you don't like that, you could declare it also in your ObjectController backing this template:
The Template:
// until 1.0 RC7
<img {{bindAttr src="newsItem.imagePath"}}></img>
// from 1.0 final (the above one is deprecated)
<img {{bind-attr src=newsItem.imagePath}}></img>
The Model:
App.NewsItem = DS.Model.extend({
imageID: DS.attr('string'),
imagePath: function() {
return "/assets/news/"+this.get("imageID")+".jpg";
}.property('imageID')
});
2- If you do not need the binding in 1 (i guess your id won't change) and use the unbound helper
, which does not result in script tags:
<img src="news{{unbound newsItem.imageID}}.jpg"></img>
You can make bindAttr reference a computed property and build your string there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With