Clean up lookupModel

This commit is contained in:
Raymond Feng 2014-01-28 18:00:12 -08:00
parent 43637a690d
commit 2a57a909f0
1 changed files with 39 additions and 32 deletions

View File

@ -18,6 +18,18 @@ Relation.relationNameFor = function relationNameFor(foreignKey) {
} }
}; };
function lookupModel(models, modelName) {
if(models[modelName]) {
return models[modelName];
}
var lookupClassName = modelName.toLowerCase();
for (var name in models) {
if (name.toLowerCase() === lookupClassName) {
return models[name];
}
}
}
/** /**
* Declare hasMany relation * Declare hasMany relation
* *
@ -34,11 +46,7 @@ Relation.hasMany = function hasMany(anotherClass, params) {
anotherClass = params.model; anotherClass = params.model;
} else { } else {
var anotherClassName = i8n.singularize(anotherClass).toLowerCase(); var anotherClassName = i8n.singularize(anotherClass).toLowerCase();
for (var name in this.dataSource.modelBuilder.models) { anotherClass = lookupModel(this.dataSource.modelBuilder.models, anotherClassName);
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.dataSource.modelBuilder.models[name];
}
}
} }
} }
var methodName = params.as || i8n.camelize(anotherClass.pluralModelName, true); var methodName = params.as || i8n.camelize(anotherClass.pluralModelName, true);
@ -130,9 +138,13 @@ Relation.hasMany = function hasMany(anotherClass, params) {
function find(id, cb) { function find(id, cb) {
anotherClass.findById(id, function (err, inst) { anotherClass.findById(id, function (err, inst) {
if (err) return cb(err); if (err) {
if (!inst) return cb(new Error('Not found')); return cb(err);
if (inst[fk] && inst[fk].toString() == this[idName].toString()) { }
if (!inst) {
return cb(new Error('Not found'));
}
if (inst[fk] && inst[fk].toString() === this[idName].toString()) {
cb(null, inst); cb(null, inst);
} else { } else {
cb(new Error('Permission denied')); cb(new Error('Permission denied'));
@ -143,9 +155,13 @@ Relation.hasMany = function hasMany(anotherClass, params) {
function destroy(id, cb) { function destroy(id, cb) {
var self = this; var self = this;
anotherClass.findById(id, function (err, inst) { anotherClass.findById(id, function (err, inst) {
if (err) return cb(err); if (err) {
if (!inst) return cb(new Error('Not found')); return cb(err);
if (inst[fk] && inst[fk].toString() == self[idName].toString()) { }
if (!inst) {
return cb(new Error('Not found'));
}
if (inst[fk] && inst[fk].toString() === self[idName].toString()) {
inst.destroy(cb); inst.destroy(cb);
} else { } else {
cb(new Error('Permission denied')); cb(new Error('Permission denied'));
@ -186,11 +202,7 @@ Relation.belongsTo = function (anotherClass, params) {
anotherClass = params.model; anotherClass = params.model;
} else { } else {
var anotherClassName = anotherClass.toLowerCase(); var anotherClassName = anotherClass.toLowerCase();
for (var name in this.dataSource.modelBuilder.models) { anotherClass = lookupModel(this.dataSource.modelBuilder.models, anotherClassName);
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.dataSource.modelBuilder.models[name];
}
}
} }
} }
@ -207,16 +219,20 @@ Relation.belongsTo = function (anotherClass, params) {
}; };
this.dataSource.defineForeignKey(this.modelName, fk, anotherClass.modelName); this.dataSource.defineForeignKey(this.modelName, fk, anotherClass.modelName);
this.prototype['__finders__'] = this.prototype['__finders__'] || {}; this.prototype.__finders__ = this.prototype.__finders__ || {};
this.prototype['__finders__'][methodName] = function (id, cb) { this.prototype.__finders__[methodName] = function (id, cb) {
if (id === null) { if (id === null) {
cb(null, null); cb(null, null);
return; return;
} }
anotherClass.findById(id, function (err, inst) { anotherClass.findById(id, function (err, inst) {
if (err) return cb(err); if (err) {
if (!inst) return cb(null, null); return cb(err);
}
if (!inst) {
return cb(null, null);
}
if (inst[idName] === this[fk]) { if (inst[idName] === this[fk]) {
cb(null, inst); cb(null, inst);
} else { } else {
@ -234,7 +250,7 @@ Relation.belongsTo = function (anotherClass, params) {
} }
var self = this; var self = this;
var cachedValue; var cachedValue;
if (!refresh && this.__cachedRelations && (typeof this.__cachedRelations[methodName] !== 'undefined')) { if (!refresh && this.__cachedRelations && (this.__cachedRelations[methodName] !== undefined)) {
cachedValue = this.__cachedRelations[methodName]; cachedValue = this.__cachedRelations[methodName];
} }
if (p instanceof ModelBaseClass) { // acts as setter if (p instanceof ModelBaseClass) { // acts as setter
@ -277,7 +293,7 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params
if (params.model) { if (params.model) {
anotherClass = params.model; anotherClass = params.model;
} else { } else {
anotherClass = lookupModel(i8n.singularize(anotherClass)) || anotherClass = lookupModel(models, i8n.singularize(anotherClass).toLowerCase()) ||
anotherClass; anotherClass;
} }
if (typeof anotherClass === 'string') { if (typeof anotherClass === 'string') {
@ -288,7 +304,7 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params
if (!params.through) { if (!params.through) {
var name1 = this.modelName + anotherClass.modelName; var name1 = this.modelName + anotherClass.modelName;
var name2 = anotherClass.modelName + this.modelName; var name2 = anotherClass.modelName + this.modelName;
params.through = lookupModel(name1) || lookupModel(name2) || params.through = lookupModel(models, name1) || lookupModel(models, name2) ||
this.dataSource.define(name1); this.dataSource.define(name1);
} }
params.through.belongsTo(this); params.through.belongsTo(this);
@ -296,13 +312,4 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params
this.hasMany(anotherClass, {as: params.as, through: params.through}); this.hasMany(anotherClass, {as: params.as, through: params.through});
function lookupModel(modelName) {
var lookupClassName = modelName.toLowerCase();
for (var name in models) {
if (name.toLowerCase() === lookupClassName) {
return models[name];
}
}
}
}; };