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