Skip to content

Commit

Permalink
feat(data): Add Model#eachAttribute (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
davewasmer authored Oct 3, 2016
1 parent dca0b04 commit a60a914
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/data/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ import {
*/
export default class Model {

/**
* An internal cache of all attributes defined on this model.
* @type {Array}
* @static
* @private
*/
static _attributesCache = null;

/**
* The type of the Model class. This string is used as the container name for
* the model, as well as in several other areas of Denali (i.e. serializers,
Expand Down Expand Up @@ -325,6 +333,29 @@ export default class Model {
return this.container.lookup(`model:${ type }`);
}

/**
* Call the supplied callback function for each attribute on this model,
* passing in the attribute name and attribute instance.
*
* @method eachAttribute
* @static
* @param fn {Function}
* @return {Array}
*/
static eachAttribute(fn) {
if (!this._attributesCache) {
this._attributesCache = [];
for (let key in this) {
if (this[key] && this[key].isAttribute) {
this._attributesCache.push(key);
}
}
}
return this._attributesCache.map((attributeName) => {
return fn(attributeName, this[attributeName]);
});
}

/**
* Lookup a model class by type.
*
Expand Down

0 comments on commit a60a914

Please sign in to comment.