建立一個新的幫助器
使用 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/
中自動建立新助手的單元測試