Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an Ember.js Handlebars template, is there a way to have both static and dynamic class attributes?

Using the already-overused to-do app example, let's say I want an element with a "todo" class (static) and an "is-done" class (dynamic):

<div {{bindAttr class="todo isDone"}}>
  Other stuff in here
</div>

In this case, "isDone" and "todo" are both expected to be properties on my view object, which is what I want for "isDone", but not for "todo". I'm currently working around this by adding a "todo" property on my view that is equal to a static "todo" string. Is there any way to have a static class attribute when using bindAttr?

Fiddle example: http://jsfiddle.net/nes4H/4/

like image 578
Adam McCrea Avatar asked Jan 10 '12 17:01

Adam McCrea


2 Answers

EDIT:

We've fixed this in Ember!

On a build from master, or after 0.9.6 is released, you can now do:

<div {{bindAttr class="App.foo:a-bound-class :a-static-class"}}></div>


Previous answer:

You unfortunately can't have both static and dynamic class names when using bindAttr.

I suggest using one or more computed properties on your view to output both static and dynamic class names.

Supporting both static and dynamic class names would be very nice, but the way bindAttr currently works, it's not possible. bindAttr doesn't know anything about the element it's being attached to during template compilation.

like image 176
ebryn Avatar answered Oct 06 '22 23:10

ebryn


I don't know if you can do it with bindAttr, but the #view helper does allow you to set both static classes and dynamic ones:

{{#view App.TodoView class="todo" classBinding="isDone"}}
  inner content
{{/view}} 
like image 27
psanford Avatar answered Oct 06 '22 22:10

psanford