Skip to content

Commit

Permalink
Added extendModel method
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed Feb 20, 2013
1 parent 7492338 commit de48183
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,38 @@ Schema.prototype.define = function defineClass(className, properties, settings)
};


/**
* Extend existing model with bunch of properties
*
* @param {String} model - name of model
* @param {Object} props - hash of properties
*
* Example:
*
* // Instead of doing this:
*
* // amend the content model with competition attributes
* db.defineProperty('Content', 'competitionType', { type: String });
* db.defineProperty('Content', 'expiryDate', { type: Date, index: true });
* db.defineProperty('Content', 'isExpired', { type: Boolean, index: true });
*
* // schema.extend allows to
* // extend the content model with competition attributes
* db.extendModel('Content', {
* competitionType: String,
* expiryDate: { type: Date, index: true },
* isExpired: { type: Boolean, index: true }
* });
*/
Schema.prototype.extendModel = function (model, props) {
var t = this;
standartize(props, {});
Object.keys(props).forEach(function (propName) {
var definition = props[propName];
t.defineProperty(model, propName, definition);
});
};

/**
* Define single property named `prop` on `model`
*
Expand Down

0 comments on commit de48183

Please sign in to comment.