0

Im still learning ember and i didn't got how i can apply certain CSS class depending on the returned string of a model on a certain controller.

The official documentation (http://emberjs.jsbin.com/yoqifiguva/1/edit?html,js,output) only covers when you have a boolean.

I didn't manage to apply this knowledge for my case. I have status string string like : "passed", "failed", "pendent" and "blocked", which i want to apply different css class for each status.

Any examples on how to achieve that?

1 Answer 1

1

You can use bind-attr, for example:

<ul>
{{#each item in model}}
  <li {{bind-attr class='item.status'}}>An item with status: `{{item.status}}`</li>
{{/each}}
</ul>

Which produces following HTML:

<ul>
  <li class="passed" data-bindattr-262="262">An item with status: `passed`</li>
  <li class="failed" data-bindattr-265="265">An item with status: `failed`</li>
</ul>

For collection:

[
  YourModel.create({ status: 'passed'}),
  YourModel.create({ status: 'failed'})
]

of objects declared like:

YourModel = Em.Object.extend({
  status: null
});

Demo.

You can also create Component which can be reused with many models. Component code:

App.StatusItemComponent = Em.Component.extend({
  classNameBindings: ['status'],

  status: function() {
    var modelStatus = this.get('model.status');
    if (modelStatus)
      return 'status-' + modelStatus;
  }.property('model.status')
});

Template code:

{{#each item in model}}
  {{#status-item model=item}}
    An item with status: {{item.status}}
  {{/status-item}}
{{/each}}

Demo.

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

4 Comments

it certainly resolve my problem but its not scalable.. i mean, if i have stats for other models that i should represent if css class this approach will not work..
You can create component with classNameBindings: ['model.status'] instead and reuse that.
Can you give a light on that?
Updated answer with Component usage example.

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.