Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combing conditional class binding with existing class

Tags:

ember.js

I am noticing an issue with conditional attribute bindings when trying to combine them with a regular class on the same element. Here is the handlebars markup I am trying:

<a href="#" class="button" {{bindAttr class="isDirty:dirty:clean"}} {{action save}}>Save</a>

What I expect to be generated is:

<a href="#" class="button clean" data-bindattr-3="3" data-ember-action="4">Save</a>

But what is actually generated is:

<a href="#" class="button" data-bindattr-3="3" data-ember-action="4">Save</a>

When I modify the model, it correctly generates the dirty class:

<a href="#" class="button dirty" data-bindattr-3="3" data-ember-action="4">Save</a>

If I try move the class after the binding, it will generate the conditional class rather than the declared one:

<a href="#" {{bindAttr class="isDirty:dirty:clean"}} class="button" {{action save}}>Save</a>

Generates the conditional but not button class:

<a href="#" class="clean" data-bindattr-3="3" data-ember-action="4">Save</a>

What I want is to have it generate both the combined declared class and the conditional class using just Handlebars (without having to create a view). Is there another way to do this?

like image 792
gbabiars Avatar asked Feb 25 '13 02:02

gbabiars


2 Answers

In the template guides it describes a way to combine static and bound classes for one item:

If you need an element to have a combination of static and bound classes, you should include the static class in the list of bound properties, prefixed by a colon.

In your case you would do something like this:

<a href="#" {{bindAttr class=":button isDirty:dirty:clean"}} {{action save}}>Save</a>

Here is a working example http://jsbin.com/ixupad/82/edit

like image 105
CraigTeegarden Avatar answered Nov 15 '22 10:11

CraigTeegarden


As of Ember 1.11 you may use inline-if-syntax

<a href="#" class="button {{if isDirty 'dirty' 'clean'}}" {{action save}}>
  Save
</a>
like image 20
Mark Fox Avatar answered Nov 15 '22 09:11

Mark Fox