adopted-ember-addons / ember-moment

MIT License
399 stars 122 forks source link

moment-format outputFormat does not support Ember.SafeString #195

Closed FredUK closed 7 years ago

FredUK commented 7 years ago

I'm trying to use a string from ember-i18n as my outputFormat on moment-format but it does not like it and moment throws an error a.match is not a function.

// locales/en/translations.js
date_format: 'DD-MM-YYYY'
// component.js
import { translationMacro as t } from 'ember-i18n';
....
testDate: moment(),
dateFormat: t('components.date_format')
//template.hbs
{{moment-format testDate dateFormat}} // Throws error

{{moment-format testDate (t 'components.date_format') // Throws error

Date format is {{dateFormat}} // Displays  'DD-MM-YYYY' as a string properly

According to ember-i18n:

Note that i18n.t returns an instance of Ember.SafeString, which is perfect for emitting to HTMLBars template"

Apparently moment-format does not support a SafeString as it just tries to pass that object into moment to render it. I think a simple check to see if the property is an instance of EmberSafeString would suffice.

image

ember.moment@4.2.1

jasonmit commented 7 years ago

I recommend creating a helper to unwrap your SafeString, or create a t-helper that returns just a string.. Otherwise, I'd need to support unwrapping all attributes and params because of ember-i18n.

In ember-intl I handle this differently by only returning a SafeString for {{format-html}}, the normal t helper returns a string since it never returns markup.