创建一个新的帮助器
使用 Ember CLI 在你的应用中生成新助手:
ember generate helper format-currency
然后编辑 helpers/format-currency.js
以包含以下内容:
import Ember from 'ember';
export function formatCurrency([value, ...rest]) {
const dollars = Math.floor(value / 100);
const cents = value % 100;
const sign = '$';
if (cents.toString().length === 1) { cents = '0' + cents; }
return `${sign}${dollars}.${cents}`;
}
export default Ember.Helper.helper(formatCurrency);
现在你可以在模板中使用 {{format-currency model.someNumericValue}}
了。
在 tests/unit/helpers/
中自动创建新助手的单元测试