Fixes in belongsTo relation definition syntax

This commit is contained in:
Anatoliy Chakkaev 2013-03-27 04:48:26 +04:00
parent d3752eab4c
commit 2749a1f7e0
1 changed files with 15 additions and 5 deletions

View File

@ -512,6 +512,12 @@ AbstractClass.include = function (objects, include, cb) {
}
var relation = relations[relationName];
if (!relation) {
return function() {
cb(new Error('Relation "' + relationName + '" is not defined for ' + self.modelName + ' model'));
}
}
var req = {'where': {}};
if (!keyVals[relation.keyFrom]) {
@ -957,10 +963,14 @@ AbstractClass.belongsTo = function (anotherClass, params) {
params = params || {};
if ('string' === typeof anotherClass) {
params.as = anotherClass;
var anotherClassName = anotherClass.toLowerCase();
for(var name in this.schema.models) {
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.schema.models[name];
if (params.model) {
anotherClass = params.model;
} else {
var anotherClassName = anotherClass.toLowerCase();
for(var name in this.schema.models) {
if (name.toLowerCase() === anotherClassName) {
anotherClass = this.schema.models[name];
}
}
}
}
@ -969,7 +979,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
this.relations[params['as']] = {
type: 'belongsTo',
keyFrom: params['foreignKey'],
keyFrom: fk,
keyTo: 'id',
modelTo: anotherClass,
multiple: false