Merge pull request #198 from offlinehacker/master

fix datasources to support new model parameters
This commit is contained in:
Raymond Feng 2014-07-30 16:26:14 -07:00
commit 52a9ab8647
1 changed files with 12 additions and 20 deletions

View File

@ -12,6 +12,7 @@ var EventEmitter = require('events').EventEmitter;
var util = require('util');
var assert = require('assert');
var async = require('async');
var traverse = require('traverse');
if (process.env.DEBUG === 'loopback') {
// For back-compatibility
@ -396,12 +397,9 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
// Check if the through model doesn't exist or resolved
if (!throughModel || isModelDataSourceAttached(throughModel)) {
// The target model is resolved
var params = {
foreignKey: relation.foreignKey,
as: name,
model: model,
options: relation.options
};
var params = traverse(relation).clone();
params.as = name;
params.model = model;
if (throughModel) {
params.through = throughModel;
}
@ -415,13 +413,10 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
throughModel.once('dataSourceAttached', function (model) {
if (isModelDataSourceAttached(targetModel)) {
// The target model is resolved
var params = {
foreignKey: relation.foreignKey,
as: name,
model: targetModel,
through: model,
options: relation.options
};
var params = traverse(relations).clone();
params.as = name;
params.model = targetModel;
params.through = model;
modelClass[relation.type].call(modelClass, name, params);
}
});
@ -438,17 +433,14 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
if (r.through) {
throughModel = isModelClass(r.through) ? r.through : this.getModel(r.through, true);
}
if (!isModelDataSourceAttached(targetModel) || (throughModel && !isModelDataSourceAttached(throughModel))) {
if ((!r.polymorphic && !isModelDataSourceAttached(targetModel)) || (throughModel && !isModelDataSourceAttached(throughModel))) {
// Create a listener to defer the relation set up
createListener(rn, r, targetModel, throughModel);
} else {
// The target model is resolved
var params = {
foreignKey: r.foreignKey,
as: rn,
model: targetModel,
options: r.options
};
var params = traverse(r).clone();
params.as = rn;
params.model = targetModel;
if (throughModel) {
params.through = throughModel;
}