Skip to content

Instantly share code, notes, and snippets.

@jmacqueen
Created October 6, 2016 16:06
Show Gist options
  • Save jmacqueen/6d31aaa5ba5f5841d123d0406f1f86c5 to your computer and use it in GitHub Desktop.
Save jmacqueen/6d31aaa5ba5f5841d123d0406f1f86c5 to your computer and use it in GitHub Desktop.
Create an Ember application instance that can be imported into any file. Useful for container lookups in files outside of the usual hierarchy.
// app/instance-initializers/application.js
import appInst from 'appName/utils/application'
export function initialize( appInstance ) {
appInst.instance = appInstance
}
export default {
name: 'application',
initialize
};
// app/validations/messages.js
//
// Sample usage. Here we are injecting the ember-i18n service into an Ember.Object
// to translate the validation messages used in ember-changeset-validations
import Ember from 'ember'
import application from 'appName/utils/application'
const Messages = Ember.Object.extend({
i18n: Ember.computed(function() {
return application.instance.lookup('service:i18n')
}),
confirmation: Ember.computed('i18n.locale', function() {
return this.get('i18n').t('validations.confirmation').toString()
}),
present: Ember.computed('i18n.locale', function() {
return this.get('i18n').t('validations.present').toString()
}),
tooShort: Ember.computed('i18n.locale', function() {
return this.get('i18n').t('validations.tooShort').toString()
}),
email: Ember.computed('i18n.locale', function() {
return this.get('i18n').t('validations.email').toString()
}),
invalid: Ember.computed('i18n.locale', function() {
return this.get('i18n').t('validations.invalid').toString()
}),
inclusion: '{description} is not included in the list',
exclusion: '{description} is reserved',
accepted: '{description} must be accepted',
empty: "{description} can't be empty",
blank: '{description} must be blank',
collection: '{description} must be a collection',
singular: "{description} can't be a collection",
tooLong: '{description} is too long (maximum is {max} characters)',
between: '{description} must be between {min} and {max} characters',
before: '{description} must be before {before}',
onOrBefore: '{description} must be on or before {onOrBefore}',
after: '{description} must be after {after}',
onOrAfter: '{description} must be on or after {onOrAfter}',
wrongDateFormat: '{description} must be in the format of {format}',
wrongLength: '{description} is the wrong length (should be {is} characters)',
notANumber: '{description} must be a number',
notAnInteger: '{description} must be an integer',
greaterThan: '{description} must be greater than {gt}',
greaterThanOrEqualTo: '{description} must be greater than or equal to {gte}',
equalTo: '{description} must be equal to {is}',
lessThan: '{description} must be less than {lt}',
lessThanOrEqualTo: '{description} must be less than or equal to {lte}',
otherThan: '{description} must be other than {value}',
odd: '{description} must be odd',
even: '{description} must be even',
positive: '{description} must be positive',
multipleOf: '{description} must be a multiple of {multipleOf}',
date: '{description} must be a valid date',
phone: '{description} must be a valid phone number',
url: '{description} must be a valid url'
})
export default Messages.create()
// app/utils/application.js
export default {
instance: null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment