Skip to content

Instantly share code, notes, and snippets.

@ixtli
Last active August 29, 2015 14:08
Show Gist options
  • Save ixtli/80d16695b6a3ecb69979 to your computer and use it in GitHub Desktop.
Save ixtli/80d16695b6a3ecb69979 to your computer and use it in GitHub Desktop.
reflection in ember js
import Ember from 'ember';
var get = Ember.get;
/**
* Invoke this as such:
*
* ```handlebars
* {{ meta-input valueBinding='myModelAttr' }}
* ```
*
* And it will expose the value of that attr AND
* all of the attribute's metadata to the hbs file
* via something akin to reflection.
*/
export default Ember.Component.extend({
valueBinding: null,
value: null,
displayName: null,
meta: null,
valueObserver: function()
{
// I guess this counts as reflection
var addr = this.get('valueBinding')._from;
var lastIndex = addr.lastIndexOf('.');
var name = addr.substr(lastIndex + 1);
var parentAddr = addr.substr(0, lastIndex);
var parent = this.get(parentAddr);
if (!parent)
{
debugger;
}
var model = parent.model ? parent.model : parent;
var attributes = get(model.constructor, 'attributes');
var meta = attributes.get(name);
// Save the model metadata
this.set('meta', meta);
// Save display name
this.set('displayName', name.decamelize().replace(/_/g, ' '));
}.observes('valueBinding').on('init')
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment