Created
December 21, 2015 18:27
-
-
Save mathieul/a66d8ec8f575cc4f85f3 to your computer and use it in GitHub Desktop.
Using ES7 decorators & async/await with Ember: example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
var app = new EmberApp(defaults, { | |
hinting: false, | |
babel: { | |
includePolyfill: true, | |
optional: [ | |
"es7.decorators", | |
"es7.classProperties", | |
"es7.asyncFunctions" | |
] | |
} | |
}); | |
// ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) : '' | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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