2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2013-04-11 23:23:34 +00:00
|
|
|
* Module exports class Model
|
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
module.exports = ModelBaseClass;
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2014-03-12 23:28:46 +00:00
|
|
|
/*!
|
2012-03-27 14:22:24 +00:00
|
|
|
* Module dependencies
|
2011-10-10 13:22:51 +00:00
|
|
|
*/
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2011-10-10 13:22:51 +00:00
|
|
|
var util = require('util');
|
2013-05-28 05:20:30 +00:00
|
|
|
var jutil = require('./jutil');
|
|
|
|
var List = require('./list');
|
|
|
|
var Hookable = require('./hooks');
|
2014-08-26 15:51:01 +00:00
|
|
|
var validations = require('./validations');
|
2014-08-20 12:03:38 +00:00
|
|
|
var _extend = util._extend;
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Set up an object for quick lookup
|
|
|
|
var BASE_TYPES = {
|
|
|
|
'String': true,
|
|
|
|
'Boolean': true,
|
|
|
|
'Number': true,
|
|
|
|
'Date': true,
|
|
|
|
'Text': true,
|
|
|
|
'ObjectID': true
|
|
|
|
};
|
2011-10-10 13:22:51 +00:00
|
|
|
|
2011-10-08 17:11:26 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Model class: base class for all persistent objects.
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2013-05-17 15:49:57 +00:00
|
|
|
* `ModelBaseClass` mixes `Validatable` and `Hookable` classes methods
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @class
|
|
|
|
* @param {Object} data Initial object data
|
2011-10-08 17:11:26 +00:00
|
|
|
*/
|
2014-02-11 06:38:59 +00:00
|
|
|
function ModelBaseClass(data, options) {
|
|
|
|
options = options || {};
|
|
|
|
if(!('applySetters' in options)) {
|
|
|
|
// Default to true
|
|
|
|
options.applySetters = true;
|
|
|
|
}
|
|
|
|
this._initProperties(data, options);
|
2012-04-19 15:01:40 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 20:06:43 +00:00
|
|
|
/**
|
2014-02-11 06:38:59 +00:00
|
|
|
* Initialize the model instance with a list of properties
|
|
|
|
* @param {Object} data The data object
|
|
|
|
* @param {Object} options An object to control the instantiation
|
|
|
|
* @property {Boolean} applySetters Controls if the setters will be applied
|
|
|
|
* @property {Boolean} strict Set the instance level strict mode
|
2014-09-05 14:35:01 +00:00
|
|
|
* @property {Boolean} persisted Whether the instance has been persisted
|
2013-07-26 20:06:43 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-02-11 06:38:59 +00:00
|
|
|
ModelBaseClass.prototype._initProperties = function (data, options) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var self = this;
|
|
|
|
var ctor = this.constructor;
|
|
|
|
|
2014-05-09 22:27:45 +00:00
|
|
|
if(data instanceof ctor) {
|
2014-06-13 06:35:20 +00:00
|
|
|
// Convert the data to be plain object to avoid pollutions
|
2014-05-09 22:27:45 +00:00
|
|
|
data = data.toObject(false);
|
|
|
|
}
|
2014-08-20 12:03:38 +00:00
|
|
|
var properties = _extend({}, ctor.definition.properties);
|
2014-01-24 17:09:53 +00:00
|
|
|
data = data || {};
|
2014-09-06 09:13:47 +00:00
|
|
|
|
|
|
|
if (typeof ctor.applyProperties === 'function') {
|
|
|
|
ctor.applyProperties(data);
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-02-11 06:38:59 +00:00
|
|
|
options = options || {};
|
|
|
|
var applySetters = options.applySetters;
|
|
|
|
var strict = options.strict;
|
|
|
|
|
|
|
|
if(strict === undefined) {
|
|
|
|
strict = ctor.definition.settings.strict;
|
|
|
|
}
|
2014-09-05 14:35:01 +00:00
|
|
|
|
2014-09-05 15:09:23 +00:00
|
|
|
if (options.persisted !== undefined) {
|
|
|
|
this.__persisted = options.persisted === true;
|
|
|
|
}
|
2014-09-05 14:35:01 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
if (ctor.hideInternalProperties) {
|
|
|
|
// Object.defineProperty() is expensive. We only try to make the internal
|
|
|
|
// properties hidden (non-enumerable) if the model class has the
|
|
|
|
// `hideInternalProperties` set to true
|
|
|
|
Object.defineProperties(this, {
|
|
|
|
__cachedRelations: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
},
|
|
|
|
|
|
|
|
__data: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: {}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Instance level data source
|
|
|
|
__dataSource: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: options.dataSource
|
|
|
|
},
|
|
|
|
|
|
|
|
// Instance level strict mode
|
|
|
|
__strict: {
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: strict
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.__cachedRelations = {};
|
|
|
|
this.__data = {};
|
|
|
|
this.__dataSource = options.dataSource;
|
|
|
|
this.__strict = strict;
|
|
|
|
}
|
2014-02-11 06:38:59 +00:00
|
|
|
|
2014-01-28 17:57:23 +00:00
|
|
|
if (data.__cachedRelations) {
|
|
|
|
this.__cachedRelations = data.__cachedRelations;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
var keys = Object.keys(data);
|
|
|
|
var size = keys.length;
|
|
|
|
var p, propVal;
|
|
|
|
for (var k = 0; k < size; k++) {
|
|
|
|
p = keys[k];
|
|
|
|
propVal = data[p];
|
|
|
|
if (typeof propVal === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (properties[p]) {
|
|
|
|
// Managed property
|
2014-09-06 12:38:57 +00:00
|
|
|
if (applySetters || properties[p].id) {
|
2014-06-13 06:35:20 +00:00
|
|
|
self[p] = propVal;
|
|
|
|
} else {
|
|
|
|
self.__data[p] = propVal;
|
|
|
|
}
|
|
|
|
} else if (ctor.relations[p]) {
|
2014-08-21 08:44:55 +00:00
|
|
|
var relationType = ctor.relations[p].type;
|
|
|
|
|
2014-08-20 12:03:38 +00:00
|
|
|
if (!properties[p]) {
|
|
|
|
var modelTo = ctor.relations[p].modelTo || ModelBaseClass;
|
|
|
|
var multiple = ctor.relations[p].multiple;
|
|
|
|
var typeName = multiple ? 'Array' : modelTo.modelName;
|
|
|
|
var propType = multiple ? [modelTo] : modelTo;
|
|
|
|
properties[p] = { name: typeName, type: propType };
|
|
|
|
this.setStrict(false);
|
|
|
|
}
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Relation
|
2014-08-21 08:44:55 +00:00
|
|
|
if (relationType === 'belongsTo' && propVal != null) {
|
2014-03-13 23:43:38 +00:00
|
|
|
// If the related model is populated
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[ctor.relations[p].keyFrom] = propVal[ctor.relations[p].keyTo];
|
2014-03-13 23:43:38 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__cachedRelations[p] = propVal;
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2014-06-13 06:35:20 +00:00
|
|
|
// Un-managed property
|
2014-08-20 12:03:38 +00:00
|
|
|
if (strict === false || self.__cachedRelations[p]) {
|
2014-08-21 20:47:09 +00:00
|
|
|
self[p] = self.__data[p] =
|
|
|
|
(propVal !== undefined) ? propVal : self.__cachedRelations[p];
|
2014-01-24 17:09:53 +00:00
|
|
|
} else if (strict === 'throw') {
|
2014-06-13 06:35:20 +00:00
|
|
|
throw new Error('Unknown property: ' + p);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2013-04-18 09:05:11 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-08-20 12:03:38 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(properties);
|
|
|
|
size = keys.length;
|
2013-08-30 23:51:17 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
for (k = 0; k < size; k++) {
|
|
|
|
p = keys[k];
|
|
|
|
propVal = self.__data[p];
|
2012-10-13 13:59:25 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Set default values
|
|
|
|
if (propVal === undefined) {
|
|
|
|
var def = properties[p]['default'];
|
|
|
|
if (def !== undefined) {
|
|
|
|
if (typeof def === 'function') {
|
2014-06-21 19:53:06 +00:00
|
|
|
if (def === Date) {
|
|
|
|
// FIXME: We should coerce the value in general
|
|
|
|
// This is a work around to {default: Date}
|
|
|
|
// Date() will return a string instead of Date
|
|
|
|
def = new Date();
|
|
|
|
} else {
|
|
|
|
def = def();
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-06-21 19:53:06 +00:00
|
|
|
// FIXME: We should coerce the value
|
|
|
|
// will implement it after we refactor the PropertyDefinition
|
|
|
|
self.__data[p] = def;
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-08-20 12:03:38 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
// Handle complex types (JSON/Object)
|
|
|
|
var type = properties[p].type;
|
2014-08-20 12:03:38 +00:00
|
|
|
if (!BASE_TYPES[type.name]) {
|
2014-08-19 20:06:55 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
if (typeof self.__data[p] !== 'object' && self.__data[p]) {
|
2014-01-24 17:09:53 +00:00
|
|
|
try {
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[p] = JSON.parse(self.__data[p] + '');
|
2014-01-24 17:09:53 +00:00
|
|
|
} catch (e) {
|
2014-06-13 06:35:20 +00:00
|
|
|
self.__data[p] = String(self.__data[p]);
|
2011-10-11 19:51:32 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-08-19 20:06:55 +00:00
|
|
|
|
|
|
|
if (type.prototype instanceof ModelBaseClass) {
|
|
|
|
if (!(self.__data[p] instanceof type)
|
|
|
|
&& typeof self.__data[p] === 'object'
|
|
|
|
&& self.__data[p] !== null ) {
|
|
|
|
self.__data[p] = new type(self.__data[p]);
|
|
|
|
}
|
|
|
|
} else if (type.name === 'Array' || Array.isArray(type)) {
|
2014-06-13 06:35:20 +00:00
|
|
|
if (!(self.__data[p] instanceof List)
|
|
|
|
&& self.__data[p] !== undefined
|
|
|
|
&& self.__data[p] !== null ) {
|
|
|
|
self.__data[p] = List(self.__data[p], type, self);
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-11 19:51:32 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
this.trigger('initialize');
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Define a property on the model.
|
|
|
|
* @param {String} prop Property name
|
|
|
|
* @param {Object} params Various property configuration
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.defineProperty = function (prop, params) {
|
2014-08-08 08:20:57 +00:00
|
|
|
if(this.dataSource) {
|
|
|
|
this.dataSource.defineProperty(this.modelName, prop, params);
|
|
|
|
} else {
|
|
|
|
this.modelBuilder.defineProperty(this.modelName, prop, params);
|
|
|
|
}
|
2011-12-09 15:23:29 +00:00
|
|
|
};
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelBaseClass.getPropertyType = function (propName) {
|
2014-01-24 17:09:53 +00:00
|
|
|
var prop = this.definition.properties[propName];
|
|
|
|
if (!prop) {
|
|
|
|
// The property is not part of the definition
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!prop.type) {
|
|
|
|
throw new Error('Type not defined for property ' + this.modelName + '.' + propName);
|
|
|
|
// return null;
|
|
|
|
}
|
|
|
|
return prop.type.name;
|
2012-05-29 11:16:24 +00:00
|
|
|
};
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
ModelBaseClass.prototype.getPropertyType = function (propName) {
|
2014-01-24 17:09:53 +00:00
|
|
|
return this.constructor.getPropertyType(propName);
|
2012-01-18 15:20:05 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
|
|
|
* Return string representation of class
|
2014-03-12 23:28:46 +00:00
|
|
|
* This overrides the default `toString()` method
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.toString = function () {
|
2014-01-24 17:09:53 +00:00
|
|
|
return '[Model ' + this.modelName + ']';
|
2012-07-13 13:53:22 +00:00
|
|
|
};
|
2011-10-08 17:11:26 +00:00
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Convert model instance to a plain JSON object.
|
|
|
|
* Returns a canonical object representation (no getters and setters).
|
2012-03-27 14:22:24 +00:00
|
|
|
*
|
2014-03-12 23:28:46 +00:00
|
|
|
* @param {Boolean} onlySchema Restrict properties to dataSource only. Default is false. If true, the function returns only properties defined in the schema; Otherwise it returns all enumerable properties.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2014-04-11 18:39:57 +00:00
|
|
|
ModelBaseClass.prototype.toObject = function (onlySchema, removeHidden) {
|
2014-06-13 06:35:20 +00:00
|
|
|
if (onlySchema === undefined) {
|
2014-01-30 19:51:34 +00:00
|
|
|
onlySchema = true;
|
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
var data = {};
|
|
|
|
var self = this;
|
2014-04-11 18:39:57 +00:00
|
|
|
var Model = this.constructor;
|
2014-01-24 17:09:53 +00:00
|
|
|
|
2014-05-05 14:23:12 +00:00
|
|
|
// if it is already an Object
|
2014-06-13 06:35:20 +00:00
|
|
|
if (Model === Object) {
|
|
|
|
return self;
|
|
|
|
}
|
2014-05-05 14:23:12 +00:00
|
|
|
|
2014-02-11 06:38:59 +00:00
|
|
|
var strict = this.__strict;
|
2014-02-04 04:52:01 +00:00
|
|
|
var schemaLess = (strict === false) || !onlySchema;
|
2014-02-11 06:38:59 +00:00
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
var props = Model.definition.properties;
|
|
|
|
var keys = Object.keys(props);
|
|
|
|
var propertyName, val;
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
propertyName = keys[i];
|
|
|
|
val = self[propertyName];
|
|
|
|
|
|
|
|
// Exclude functions
|
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
2014-05-09 22:27:45 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
// Exclude hidden properties
|
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
|
|
|
continue;
|
2014-05-09 22:27:45 +00:00
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
|
|
|
|
if (val instanceof List) {
|
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
|
|
|
} else {
|
|
|
|
if (val !== undefined && val !== null && val.toObject) {
|
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
2014-01-24 17:09:53 +00:00
|
|
|
} else {
|
2014-06-13 06:35:20 +00:00
|
|
|
data[propertyName] = val;
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
if (schemaLess) {
|
2014-01-30 19:51:34 +00:00
|
|
|
// Find its own properties which can be set via myModel.myProperty = 'myValue'.
|
|
|
|
// If the property is not declared in the model definition, no setter will be
|
|
|
|
// triggered to add it to __data
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(self);
|
|
|
|
var size = keys.length;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
propertyName = keys[i];
|
|
|
|
if (props[propertyName]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (propertyName.indexOf('__') === 0) {
|
2014-05-09 22:27:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
val = self[propertyName];
|
|
|
|
if (val !== undefined && data[propertyName] === undefined) {
|
2014-05-09 22:27:45 +00:00
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
if (val !== null && val.toObject) {
|
2014-04-11 18:39:57 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
2014-01-30 19:51:34 +00:00
|
|
|
} else {
|
|
|
|
data[propertyName] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Now continue to check __data
|
2014-06-13 06:35:20 +00:00
|
|
|
keys = Object.keys(self.__data);
|
|
|
|
size = keys.length;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
propertyName = keys[i];
|
|
|
|
if (propertyName.indexOf('__') === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (data[propertyName] === undefined) {
|
|
|
|
if (removeHidden && Model.isHiddenProperty(propertyName)) {
|
2014-05-09 22:27:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
var ownVal = self[propertyName];
|
|
|
|
// The ownVal can be a relation function
|
|
|
|
val = (ownVal !== undefined && (typeof ownVal !== 'function'))
|
|
|
|
? ownVal : self.__data[propertyName];
|
2014-05-09 22:27:45 +00:00
|
|
|
if (typeof val === 'function') {
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-13 06:35:20 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
if (val !== undefined && val !== null && val.toObject) {
|
2014-04-11 18:39:57 +00:00
|
|
|
data[propertyName] = val.toObject(!schemaLess, removeHidden);
|
2012-09-10 15:57:21 +00:00
|
|
|
} else {
|
2014-01-24 17:09:53 +00:00
|
|
|
data[propertyName] = val;
|
2013-10-07 04:27:02 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2012-10-13 13:59:25 +00:00
|
|
|
}
|
2014-01-24 17:09:53 +00:00
|
|
|
}
|
2014-04-11 18:39:57 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
return data;
|
2011-10-08 17:11:26 +00:00
|
|
|
};
|
|
|
|
|
2014-06-13 06:35:20 +00:00
|
|
|
ModelBaseClass.isHiddenProperty = function (propertyName) {
|
2014-04-11 18:39:57 +00:00
|
|
|
var Model = this;
|
|
|
|
var settings = Model.definition && Model.definition.settings;
|
2014-06-13 06:35:20 +00:00
|
|
|
var hiddenProperties = settings && (settings.hiddenProperties || settings.hidden);
|
|
|
|
if (Array.isArray(hiddenProperties)) {
|
|
|
|
// Cache the hidden properties as an object for quick lookup
|
|
|
|
settings.hiddenProperties = {};
|
|
|
|
for (var i = 0; i < hiddenProperties.length; i++) {
|
|
|
|
settings.hiddenProperties[hiddenProperties[i]] = true;
|
|
|
|
}
|
|
|
|
hiddenProperties = settings.hiddenProperties;
|
|
|
|
}
|
|
|
|
if (hiddenProperties) {
|
|
|
|
return hiddenProperties[propertyName];
|
2014-04-11 18:39:57 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.toJSON = function () {
|
2014-04-11 18:39:57 +00:00
|
|
|
return this.toObject(false, true);
|
2012-09-10 15:57:21 +00:00
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.fromObject = function (obj) {
|
2014-01-24 17:09:53 +00:00
|
|
|
for (var key in obj) {
|
|
|
|
this[key] = obj[key];
|
|
|
|
}
|
2012-04-09 16:24:35 +00:00
|
|
|
};
|
|
|
|
|
2012-03-27 14:22:24 +00:00
|
|
|
/**
|
2014-03-12 23:28:46 +00:00
|
|
|
* Reset dirty attributes.
|
|
|
|
* This method does not perform any database operations; it just resets the object to its
|
|
|
|
* initial state.
|
2012-03-27 14:22:24 +00:00
|
|
|
*/
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.reset = function () {
|
2014-01-24 17:09:53 +00:00
|
|
|
var obj = this;
|
|
|
|
for (var k in obj) {
|
|
|
|
if (k !== 'id' && !obj.constructor.dataSource.definitions[obj.constructor.modelName].properties[k]) {
|
|
|
|
delete obj[k];
|
|
|
|
}
|
|
|
|
}
|
2011-11-28 16:31:01 +00:00
|
|
|
};
|
|
|
|
|
2013-05-17 15:49:57 +00:00
|
|
|
ModelBaseClass.prototype.inspect = function () {
|
2014-01-24 17:09:53 +00:00
|
|
|
return util.inspect(this.__data, false, 4, true);
|
2012-10-15 23:15:29 +00:00
|
|
|
};
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
ModelBaseClass.mixin = function (anotherClass, options) {
|
2014-08-06 11:26:47 +00:00
|
|
|
if (typeof anotherClass === 'string') {
|
2014-08-08 08:20:57 +00:00
|
|
|
this.modelBuilder.mixins.applyMixin(this, anotherClass, options);
|
2014-08-06 11:26:47 +00:00
|
|
|
} else {
|
2014-08-08 08:20:57 +00:00
|
|
|
if (anotherClass.prototype instanceof ModelBaseClass) {
|
|
|
|
var props = anotherClass.definition.properties;
|
|
|
|
for (var i in props) {
|
|
|
|
if (this.definition.properties[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this.defineProperty(i, props[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 11:26:47 +00:00
|
|
|
return jutil.mixin(this, anotherClass, options);
|
|
|
|
}
|
2013-10-02 05:14:21 +00:00
|
|
|
};
|
2013-05-28 20:50:59 +00:00
|
|
|
|
2013-10-31 17:51:33 +00:00
|
|
|
ModelBaseClass.prototype.getDataSource = function () {
|
|
|
|
return this.__dataSource || this.constructor.dataSource;
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
|
|
|
|
2013-10-31 17:51:33 +00:00
|
|
|
ModelBaseClass.getDataSource = function () {
|
|
|
|
return this.dataSource;
|
2014-02-11 06:38:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ModelBaseClass.prototype.setStrict = function (strict) {
|
|
|
|
this.__strict = strict;
|
|
|
|
};
|
2013-10-31 17:51:33 +00:00
|
|
|
|
2013-05-28 05:20:30 +00:00
|
|
|
jutil.mixin(ModelBaseClass, Hookable);
|
2013-10-04 21:32:51 +00:00
|
|
|
jutil.mixin(ModelBaseClass, validations.Validatable);
|