Fix options for hasManyThrough doesn't apply

Signed-off-by: Clark Wang <clark.wangs@gmail.com>
This commit is contained in:
Clark Wang 2014-09-01 23:59:52 +08:00
parent f9a26bcb28
commit 4b70f2498f
2 changed files with 22 additions and 1 deletions

View File

@ -417,7 +417,7 @@ DataSource.prototype.defineRelations = function (modelClass, relations) {
throughModel.once('dataAccessConfigured', function (model) { throughModel.once('dataAccessConfigured', function (model) {
if (isModelDataSourceAttached(targetModel)) { if (isModelDataSourceAttached(targetModel)) {
// The target model is resolved // The target model is resolved
var params = traverse(relations).clone(); var params = traverse(relation).clone();
params.as = name; params.as = name;
params.model = targetModel; params.model = targetModel;
params.through = model; params.through = model;

View File

@ -1110,6 +1110,27 @@ describe('Load models with relations', function () {
done(); done();
}); });
it('should handle hasMany through options', function (done) {
var ds = new DataSource('memory');
var Physician = ds.createModel('Physician', {
name: String
}, {relations: {patients: {model: 'Patient', type: 'hasMany', foreignKey: 'leftId', through: 'Appointment'}}});
var Patient = ds.createModel('Patient', {
name: String
}, {relations: {physicians: {model: 'Physician', type: 'hasMany', foreignKey: 'rightId', through: 'Appointment'}}});
var Appointment = ds.createModel('Appointment', {
physicianId: Number,
patientId: Number,
appointmentDate: Date
}, {relations: {patient: {type: 'belongsTo', model: 'Patient'}, physician: {type: 'belongsTo', model: 'Physician'}}});
assert(Physician.relations['patients'].keyTo === 'leftId');
assert(Patient.relations['physicians'].keyTo === 'rightId');
done();
});
it('should set up relations after attach', function (done) { it('should set up relations after attach', function (done) {
var ds = new DataSource('memory'); var ds = new DataSource('memory');
var modelBuilder = new ModelBuilder(); var modelBuilder = new ModelBuilder();