Move resolveType to ModelBuilder

This commit is contained in:
Raymond Feng 2013-10-17 14:23:29 -07:00
parent dd936b15a2
commit 32dbe9cb21
2 changed files with 42 additions and 42 deletions

View File

@ -275,7 +275,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
return new OrigDate(arg);
};
} else if(typeof DataType === 'string') {
DataType = dataSource.getSchemaType(DataType);
DataType = dataSource.resolveType(DataType);
}
if(prop.required) {
@ -449,6 +449,46 @@ ModelBuilder.prototype.getSchemaName = function (name) {
return 'AnonymousModel_' + this._nameCount;
};
/**
* Resolve the type string to be a function, for example, 'String' to String
* @param {String} type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
* @returns {Function} if the type is resolved
*/
ModelBuilder.prototype.resolveType = function(type) {
if (!type) {
return type;
}
if (Array.isArray(type) && type.length > 0) {
// For array types, the first item should be the type string
var itemType = this.resolveType(type[0]);
if (typeof itemType === 'function') {
return [itemType];
}
else {
return itemType; // Not resolved, return the type string
}
}
if (typeof type === 'string') {
var schemaType = ModelBuilder.schemaTypes[type.toLowerCase()] || this.models[type];
if (schemaType) {
return schemaType;
} else {
return type;
}
} else if (type.constructor.name === 'Object') {
// We also support the syntax {type: 'string', ...}
if (type.type) {
return this.resolveType(type.type);
} else {
return this.define(this.getSchemaName(null),
type, {anonymous: true, idInjection: false});
}
} else if('function' === typeof type ) {
return type;
}
return type;
};
/**
* Build models from dataSource definitions
*

View File

@ -201,46 +201,6 @@ ModelDefinition.prototype.indexes = function () {
return indexes;
};
/**
* Resolve the type string to be a function, for example, 'String' to String
* @param {String} type The type string, such as 'number', 'Number', 'boolean', or 'String'. It's case insensitive
* @returns {Function} if the type is resolved
*/
ModelDefinition.prototype.resolveType = function(type) {
if (!type) {
return type;
}
if (Array.isArray(type) && type.length > 0) {
// For array types, the first item should be the type string
var itemType = this.resolveType(type[0]);
if (typeof itemType === 'function') {
return [itemType];
}
else {
return itemType; // Not resolved, return the type string
}
}
if (typeof type === 'string') {
var schemaType = ModelDefinition.schemaTypes[type.toLowerCase()] || this.modelBuilder.models[type];
if (schemaType) {
return schemaType;
} else {
return type;
}
} else if (type.constructor.name === 'Object') {
// We also support the syntax {type: 'string', ...}
if (type.type) {
return this.resolveType(type.type);
} else {
return this.modelBuilder.define(this.modelBuilder.getSchemaName(null),
type, {anonymous: true, idInjection: false});
}
} else if('function' === typeof type ) {
return type;
}
return type;
};
/**
* Build a model definition
* @param {Boolean} force Forcing rebuild
@ -257,7 +217,7 @@ ModelDefinition.prototype.build = function (forceRebuild) {
this.properties = {};
for (var p in this.rawProperties) {
var prop = this.rawProperties[p];
var type = this.resolveType(prop);
var type = this.modelBuilder.resolveType(prop);
if (typeof type === 'string') {
this.associations.push({
source: this.name,