AJP
AJP

Reputation: 545

How do I display specific string instead of model value in template using Ember.js?

I have an Ember route displaying an array of records. The model includes status like so:

status: DS.attr('number'),

The value of status will be returned as either: -1, 0 or 1.

I have no problem displaying that value for each record in the template using handlebars: {{modelName.status}}. This way, the status for each record reflects as either: -1, 0, 1.

I would like to do the following:

If the value of status is -1 display the string "Error".
If the value of status is 0 display the string "Completed".
If the value of status is 1 display the string "Pending".

Is this possible?

Upvotes: 1

Views: 114

Answers (3)

AJP
AJP

Reputation: 545

A small addition to the first answer- let's say my needs are:

If the value of status is -1 display the string "Error".
If the value of status is 0 display the string "Completed".
If the value of status is anything greater than 0 display the string "Pending".

statusLabel: function() {
  var currentStatus = this.get('status');
  var statuses = ['Error', 'Completed', 'Pending'];
  if (currentStatus > 0) {
    return statuses[2];
  } else {
    return statuses[this.get('status') + 1];
  }
}.property('status'),

Upvotes: 0

Nitesh singh
Nitesh singh

Reputation: 941

your question is not to much clear, for my options Ember trust helper will help you. https://github.com/jmurphyau/ember-truth-helpers.

you need to install Ember trust helper.

{{if (not-eq modelName.status -1 )}} Error

Upvotes: 1

Kit Sunde
Kit Sunde

Reputation: 37075

How about:

status: DS.attr('number'),
statusLabel: function(){
  var statuses = ['Error', 'Completed', 'Pending'];
  return statuses[this.get('status') + 1];
}.property('status')

Then in your template:

{{modelName.statusLabel}}

Upvotes: 1

Related Questions