rjhilgefort
11/15/2014 - 3:41 AM

Ember Object Property List Strategy

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;