Skip to content

Instantly share code, notes, and snippets.

@rjhilgefort
Last active August 29, 2015 14:09
Show Gist options
  • Save rjhilgefort/e404d0ecbe68b6a3fcee to your computer and use it in GitHub Desktop.
Save rjhilgefort/e404d0ecbe68b6a3fcee to your computer and use it in GitHub Desktop.
Ember Object Property List Strategy
var AppObject = Ember.Object.extend({
/**
* Get a list of all computed properties set on this instance
*
* @method getComputedPropertyList
* @return {Array} List of computed properties (strings)
* @since TODO
*/
getComputedPropertyList: function() {
// TODO: This will not return computed properties that have been set at run time
return this.constructor.getComputedPropertyList();
},
/**
* Get a list of all POJOs on this instance
*
* @method getPropertyList
* @return {Array} List of properties (strings)
* @since TODO
*/
getPropertyList: function() {
var properties,
typeBlacklist = ['class', 'function'],
propertyBlacklist = [
'__nextSuper', 'validations', 'errors',
'_dependentValidationKeys', 'validators'
];
// Properties declared on the model
properties = Em.keys(this.constructor.prototype);
// Properties that have been set at run time
properties = _.union(properties, Em.keys(this));
// Remove computed properties
properties = _.difference(properties, this.getComputedPropertyList());
properties = _.filter(properties, function(property) {
// Filter out undesirable types
if (_.contains(typeBlacklist, Em.typeOf(this.get(property)))) return false;
// Filter out undesirable properties
if (_.contains(propertyBlacklist, property)) return false;
// TODO: revisit this strategy
// Strip out "private" properties designated by a leading underscore
// We've started doing this as a convention (in the user model), definitely experimental.
if (property[0] === '_') return false;
return true;
}, this);
return properties;
}
});
AppObject.reopenClass({
/**
* Get a list of all computed properties built on this model
* NOTE: Does not include computed properties set at run time
*
* @method getComputedPropertyList
* @return {Array} List of computed properties (strings)
* @since TODO
*/
getComputedPropertyList: function() {
var properties = [];
this.eachComputedProperty(function(key, value) {
properties.push(key);
});
return properties;
}
});
export default AppObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment