Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Last active June 14, 2017 09:58
Show Gist options
  • Save chris-kobrzak/1acd4b9a46c56337f23ed6bccbec93f4 to your computer and use it in GitHub Desktop.
Save chris-kobrzak/1acd4b9a46c56337f23ed6bccbec93f4 to your computer and use it in GitHub Desktop.
[WIP] Use ES6 classes instead of object literals in Ember.js
export default function buildObjectFromClass (className) {
return Object.getOwnPropertyNames(className.prototype)
.filter(propertyName => propertyName != 'constructor')
.reduce((result, propertyName) => {
result[propertyName] = className.prototype[propertyName]
return result
}, {})
}
export default function buildObjectFromInstance (instance) {
return Object.getOwnPropertyNames(instance)
.reduce((result, propertyName) => {
result[propertyName] = instance[propertyName]
return result
}, {})
}
import Ember from 'ember'
import buildObjectFromInstance from '../utils/build-object-from-instance'
class Scientists {
constructor () {
this.nameFormat = 'full'
}
}
const scientists = new Scientists()
const scientistsObjectLiteral = buildObjectFromInstance(scientists)
export default Ember.Controller.extend(scientistsObjectLiteral)
import Ember from 'ember'
import buildObjectFromClass from '../utils/build-object-from-class'
class Scientists {
model () {
return ['Marie Curie', 'Mae Jemison', 'Albert Hofmann']
}
}
const scientistsObjectLiteral = buildObjectFromClass(Scientists)
export default Ember.Route.extend(scientistsObjectLiteral)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment