From 2a57a909f04e088887dfc570ef26ccf64a1e9116 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Tue, 28 Jan 2014 18:00:12 -0800 Subject: [PATCH] Clean up lookupModel --- lib/relations.js | 71 ++++++++++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/lib/relations.js b/lib/relations.js index d3588168..a5372f92 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -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 * @@ -34,11 +46,7 @@ Relation.hasMany = function hasMany(anotherClass, params) { anotherClass = params.model; } else { var anotherClassName = i8n.singularize(anotherClass).toLowerCase(); - for (var name in this.dataSource.modelBuilder.models) { - if (name.toLowerCase() === anotherClassName) { - anotherClass = this.dataSource.modelBuilder.models[name]; - } - } + anotherClass = lookupModel(this.dataSource.modelBuilder.models, anotherClassName); } } var methodName = params.as || i8n.camelize(anotherClass.pluralModelName, true); @@ -130,9 +138,13 @@ Relation.hasMany = function hasMany(anotherClass, params) { function find(id, cb) { anotherClass.findById(id, function (err, inst) { - if (err) return cb(err); - if (!inst) return cb(new Error('Not found')); - if (inst[fk] && inst[fk].toString() == this[idName].toString()) { + if (err) { + return cb(err); + } + if (!inst) { + return cb(new Error('Not found')); + } + if (inst[fk] && inst[fk].toString() === this[idName].toString()) { cb(null, inst); } else { cb(new Error('Permission denied')); @@ -143,9 +155,13 @@ Relation.hasMany = function hasMany(anotherClass, params) { function destroy(id, cb) { var self = this; anotherClass.findById(id, function (err, inst) { - if (err) return cb(err); - if (!inst) return cb(new Error('Not found')); - if (inst[fk] && inst[fk].toString() == self[idName].toString()) { + if (err) { + return cb(err); + } + if (!inst) { + return cb(new Error('Not found')); + } + if (inst[fk] && inst[fk].toString() === self[idName].toString()) { inst.destroy(cb); } else { cb(new Error('Permission denied')); @@ -186,11 +202,7 @@ Relation.belongsTo = function (anotherClass, params) { anotherClass = params.model; } else { var anotherClassName = anotherClass.toLowerCase(); - for (var name in this.dataSource.modelBuilder.models) { - if (name.toLowerCase() === anotherClassName) { - anotherClass = this.dataSource.modelBuilder.models[name]; - } - } + anotherClass = lookupModel(this.dataSource.modelBuilder.models, anotherClassName); } } @@ -207,16 +219,20 @@ Relation.belongsTo = function (anotherClass, params) { }; 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) { cb(null, null); return; } anotherClass.findById(id, function (err, inst) { - if (err) return cb(err); - if (!inst) return cb(null, null); + if (err) { + return cb(err); + } + if (!inst) { + return cb(null, null); + } if (inst[idName] === this[fk]) { cb(null, inst); } else { @@ -234,7 +250,7 @@ Relation.belongsTo = function (anotherClass, params) { } var self = this; var cachedValue; - if (!refresh && this.__cachedRelations && (typeof this.__cachedRelations[methodName] !== 'undefined')) { + if (!refresh && this.__cachedRelations && (this.__cachedRelations[methodName] !== undefined)) { cachedValue = this.__cachedRelations[methodName]; } if (p instanceof ModelBaseClass) { // acts as setter @@ -277,7 +293,7 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params if (params.model) { anotherClass = params.model; } else { - anotherClass = lookupModel(i8n.singularize(anotherClass)) || + anotherClass = lookupModel(models, i8n.singularize(anotherClass).toLowerCase()) || anotherClass; } if (typeof anotherClass === 'string') { @@ -288,7 +304,7 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params if (!params.through) { var name1 = this.modelName + anotherClass.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); } params.through.belongsTo(this); @@ -296,13 +312,4 @@ Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params 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]; - } - } - } - };