Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ember.js templates, how does one print out model's property that will be use by HTML like the src for image or href for link

Using:

  • ember: v1.0.0-pre.4
  • ember-data: revision 11
  • handlebars: 1.0.rc2

So I have a template that I've hooked up to a controller that's receiving info from an REST API. In just print out text, this is fine but these handlebar expression...

<img src="{{ imageUrl }}"/> 

...when inserted into the dom look like:

<img src="&lt;script id='metamorph-28-start' type='text/x-placeholder'&gt;&lt;/script&gt;http://asdf.com/image.jpg&lt;script id='metamorph-28-end' type='text/x-placeholder'&gt;&lt;/script&gt;">

I'm obviously very new to Ember.js and Handlebars.

I've tried doing searches for "rendering urls in ember templates" and "print out html in ember mustache templates." Ack, probably obvious but I'm missing it.

like image 571
Jinyoung Kim Avatar asked Jan 29 '13 19:01

Jinyoung Kim


People also ask

What is model in Ember js?

In Ember Data, models are objects that represent the underlying data that your application presents to the user. Note that Ember Data models are a different concept than the model method on Routes, although they share the same name.

How does Ember js work?

Ember uses templates to organize the layout of HTML in an application. Ember templates use the syntax of Handlebars templates. Anything that is valid Handlebars syntax is valid Ember syntax. Here, {{name}} is a property provided by the template's context.

What is Ember template?

At its core, Ember's UIs are HTML driven - every part of the UI that is shown to the user is defined in an HTML template somewhere in your application. Because of this, templates are central to Ember, and one of the most important parts of the framework.

What is Handlebars template in Ember?

Ember uses the Handlebars templating library to power your app's user interface. Handlebars templates contain static HTML and dynamic content inside Handlebars expressions, which are invoked with double curly braces: {{}} . Dynamic content inside a Handlebars expression is rendered with data-binding.


2 Answers

try this:

<img {{bind-attr src="imageUrl"}} />

but you can have more than just one attribute like:

<img {{bind-attr src="imageUrl" alt="imageTitle"}}>

here is the doc: http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_bind-attr

also, can be useful in some cases where you don't need the variable to be bound, you could use:

<img src="{{unbound imageUrl}}" />

ref to the doc: http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_unbound

but the first method is usually the best.

like image 97
colymba Avatar answered Sep 23 '22 15:09

colymba


Taking this a little bit further to get your feet even more wet, we can actually create a view that represents an image, and use that to add more functionality.

For instance, in the following JSFiddle I've set the view's tagName to img (where its default is a div), and then added an attributeBindings to bind attributes to the view. In our case we want to bind to the src attribute. All we then need to do is specify the src attribute as a property of the view, and give it a default value -- in this example, Google.

Therefore the view works as expected: we've displayed an image as part of a view.

However, taking it one step further, we can now easily change the image's src attribute by using the .set() method. On the magical click event, which is invoked when the user clicks on the view in the DOM (try it yourself by clicking on Google's logo in the JSFiddle!), the src attribute is changed from Google's logo to Yahoo's logo. Since we're observing the src attribute from attributeBindings, this is updated as soon as we call:

this.set('src', 'http://l.yimg.com/dh/ap/default/120910/yahoo_logo_br.png');

Full view code in case JSFiddle disappears:

App.ImageView = Ember.View.extend({
    tagName: 'img',
    attributeBindings: ['src'],
    src: 'https://www.google.com/images/srpr/logo3w.png',
    click: function() {
        this.set('src', 'http://l.yimg.com/dh/ap/default/120910/yahoo_logo_br.png'); 
    }
});
like image 22
Wildhoney Avatar answered Sep 21 '22 15:09

Wildhoney