Skip to content

Instantly share code, notes, and snippets.

@mathieul
Created December 21, 2015 18:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mathieul/a66d8ec8f575cc4f85f3 to your computer and use it in GitHub Desktop.
Save mathieul/a66d8ec8f575cc4f85f3 to your computer and use it in GitHub Desktop.
Using ES7 decorators & async/await with Ember: example
// ...
var app = new EmberApp(defaults, {
hinting: false,
babel: {
includePolyfill: true,
optional: [
"es7.decorators",
"es7.classProperties",
"es7.asyncFunctions"
]
}
});
// ...
// app/models/project.js
import Ember from 'ember'
import DS from 'ember-data'
import computed from 'ember-computed-decorators'
import { attr } from 'ember-computed-decorators/ember-data'
const { Model } = DS
const { String: { capitalize } } = Ember
export default Model.extend({
@attr('string') name,
@computed('name')
capitalizedName(name) {
return name ? capitalize(name) : ''
}
})
// app/pods/register/route.js
import Ember from 'ember'
import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin'
const {
Route,
inject: { service }
} = Ember
export default Route.extend(UnauthenticatedRouteMixin, {
ajax: service(),
actions: {
async register(name, email, password) {
try {
await this.get('ajax').request('/api/users', {
type: 'POST',
data: {user: {email, password, name}}
})
this.setProperties({name: '', email: '', password: ''})
this.transitionTo('login')
} catch({errors}) {
this.set('errors', errors)
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment