20

This is solved since Ember 1.8 with the HTMLBars engine.

I would like to bind a css style in a template. What would be the solution ?

I tried this:

<div class="bar" style="width:{{barWidth}}px"></div>

but DOM element look like this after its been rendered:

<div class="bar" style="width:<script id='metamorph-28-start' type='text/x-placeholder'></script>5.000000000000002<script

id='metamorph-28-end' type='text/x-placeholder'>px">

Obviously here we can see that the tag for metamorph was added within the style attribute...

I'm wondering what is the best way to achieve such things with Ember.js


There is something i don't get yet.

I have a template as follow:

<script type="text/x-handlebars" data-template-name="listTemplate">
<ul id="list">
    {{#each App.list}}
      <li {{bindAttr data-item-id="itemId"}}>
        <div>
            <span class="label">{{itemName}}</span>
            <div class="barContainer">
              <div class="bar" {{bindAttr style="barWidth"}}></div>   
              <div class="barCap"></div>
            </div>
        </div>
      </li>
    {{/each}}
    </ul>

i'm in a for each loop that loops thru my ArrayProxy content... and the bar width vary depending of the value of each item in the list. Your solution here will not work as the view is the UL and i need a barWidth per model item. and I do not want to polute my Model with css related things like "width: ###px"

Is there any other elegant way to solve what i try to do ? maybe it would be radically different. I'm very new to ember.js and try to discover the best-practices yet:)

4 Answers 4

15

Set a string on your view that looks like "width: 100px" and then bind it to your div with the bind-attr helper like so: <div {{bind-attr style="divStyle"}}>Test</div>

http://jsfiddle.net/tomwhatmore/rearT/1/

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for this fast reply however there is still something i don't get.
I updated my original question .. I'm not able to answer to my own answer for 7 more hours.
Assuming your model does have the value on it then the solution is for the view to have a method that transforms that value into the appropriate css string.
I created an Handlebar Helper for ember.js based on bindAttr called bindStyle. {{bindStyle width="value1" height="value2"}} jsfiddle.net/yderidder/a9HsE
This said, I'm not sure it is the best solution... I have the value in my Model but as i said, I do not have one View per <li> but I have one view for the <ul> so if I follow what your suggestion, i would need a function in my View that takes the value in its argument and pass back a string such as "width:###px" but how do i call this function from my template passing the argument ? Or am i missing something here :)
|
14

To simplify all that, I created a tiny handlebars helper for emberjs that allows you to bind any style properties. You can look at https://github.com/yderidde/bindstyle-ember-helper

1 Comment

This should now be marked as the correct answer. The original answer provided no longer works with Ember 1.0.
11

Add unbound:

<div class="bar" style="width:{{unbound barWidth}}px"></div>

2 Comments

Well it works but you have to be careful because you don't get automatic binding with the unbound helper.
Agreed, the question was regarding keeping the attribute bound, not explicitly unbinding it.
1

In Ember 2.0:

<div class="bar" style="width:{{barWidth}}px"></div>

will just work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.