Skip to content

Instantly share code, notes, and snippets.

@locks
Forked from jmacqueen/application.js
Last active July 28, 2017 09:47
Show Gist options
  • Save locks/0efd9458aab322f031a1ba06d6e3884c to your computer and use it in GitHub Desktop.
Save locks/0efd9458aab322f031a1ba06d6e3884c 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.inject.service(),
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(application.instance.ownerInjection());
// 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