Created
April 3, 2015 15:59
-
-
Save marcioj/17b48c7dc74a0fcc1739 to your computer and use it in GitHub Desktop.
DS.Model using javascript decorators
This file contains 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
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