简体   繁体   English

如何使用Ember.js在模板中显示特定的字符串而不是模型值?

[英]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. 我有一条Ember路线,显示一组记录。 The model includes status like so: 该模型包括如下status

status: DS.attr('number'),

The value of status will be returned as either: -1 , 0 or 1 . 的状态的值将返回为任一: -101

I have no problem displaying that value for each record in the template using handlebars: {{modelName.status}} . 我没问题使用{{modelName.status}}来显示模板中每个记录的值。 This way, the status for each record reflects as either: -1 , 0 , 1 . 这样一来,每条记录的状态反映为两种: -101

I would like to do the following: 我要执行以下操作:

If the value of status is -1 display the string "Error". 如果status的值为-1,则显示字符串“ Error”。
If the value of status is 0 display the string "Completed". 如果status的值为0,则显示字符串“ Completed”。
If the value of status is 1 display the string "Pending". 如果status的值为1,则显示字符串“ Pending”。

Is this possible? 这可能吗?

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}}

your question is not to much clear, for my options Ember trust helper will help you. 您的问题不太清楚,因为我的选择是Ember信任帮助程序将为您提供帮助。 https://github.com/jmurphyau/ember-truth-helpers . https://github.com/jmurphyau/ember-truth-helpers

you need to install Ember trust helper. 您需要安装Ember Trust Helper。

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

A small addition to the first answer- let's say my needs are: 除了第一个答案之外,我们的需求还包括:

If the value of status is -1 display the string "Error". 如果status的值为-1,则显示字符串“ Error”。
If the value of status is 0 display the string "Completed". 如果status的值为0,则显示字符串“ Completed”。
If the value of status is anything greater than 0 display the string "Pending". 如果status的值大于0,则显示字符串“ 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'), 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM