Skip to content

Instantly share code, notes, and snippets.

@marcioj
Created April 3, 2015 15:59
Show Gist options
  • Save marcioj/17b48c7dc74a0fcc1739 to your computer and use it in GitHub Desktop.
Save marcioj/17b48c7dc74a0fcc1739 to your computer and use it in GitHub Desktop.
DS.Model using javascript decorators
import DS from 'ember-data';
function model(clazz) {
var propertyNames = Object.getOwnPropertyNames(clazz.prototype);
var hash = {}
propertyNames.forEach( (prop) => {
if (prop !== "constructor") {
hash[prop] = Object.getOwnPropertyDescriptor(clazz.prototype, prop).value;
}
})
return DS.Model.extend(hash);
}
function attr(type, options) {
return function(clazz, key, descriptor) {
clazz[key] = DS.attr(type);
}
}
function hasMany(association, options) {
return function(clazz, key, descriptor) {
clazz[key] = DS.hasMany(association, options);
}
}
@model
export default class User {
@attr('string')
name
@hasMany('post', { async: true })
posts
}
// Is equivalent to
//
// export default DS.Model.extend({
// name: DS.attr('string'),
// posts: DS.hasMany('post', { async: true })
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment