Test build of embedsMany
This commit is contained in:
parent
cd2cc68905
commit
43e11af942
|
@ -1614,6 +1614,39 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
|
|||
var modelInstance = this.modelInstance;
|
||||
var autoId = this.definition.options.autoId !== false;
|
||||
|
||||
if (typeof targetModelData === 'function' && !cb) {
|
||||
cb = targetModelData;
|
||||
targetModelData = {};
|
||||
}
|
||||
targetModelData = targetModelData || {};
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
|
||||
var inst = this.build(targetModelData);
|
||||
|
||||
var err = inst.isValid() ? null : new ValidationError(inst);
|
||||
|
||||
if (err) {
|
||||
var index = embeddedList.indexOf(inst);
|
||||
if (index > -1) embeddedList.splice(index, 1);
|
||||
return process.nextTick(function() {
|
||||
cb(err, embeddedList);
|
||||
});
|
||||
}
|
||||
|
||||
modelInstance.updateAttribute(relationName,
|
||||
embeddedList, function(err, modelInst) {
|
||||
cb(err, modelInst[relationName]);
|
||||
});
|
||||
};
|
||||
|
||||
EmbedsMany.prototype.build = HasOne.prototype.build = function(targetModelData) {
|
||||
var pk = this.definition.keyFrom;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var modelInstance = this.modelInstance;
|
||||
var autoId = this.definition.options.autoId !== false;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
|
||||
if (typeof targetModelData === 'function' && !cb) {
|
||||
|
@ -1631,34 +1664,6 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
|
|||
|
||||
this.definition.applyProperties(this.modelInstance, targetModelData);
|
||||
|
||||
var inst = new modelTo(targetModelData);
|
||||
var err = inst.isValid() ? null : new ValidationError(inst);
|
||||
|
||||
if (err) {
|
||||
return process.nextTick(function() {
|
||||
cb(err, embeddedList);
|
||||
});
|
||||
} else if (this.definition.options.prepend) {
|
||||
embeddedList.unshift(inst);
|
||||
} else {
|
||||
embeddedList.push(inst);
|
||||
}
|
||||
|
||||
modelInstance.updateAttribute(relationName,
|
||||
embeddedList, function(err, modelInst) {
|
||||
cb(err, modelInst[relationName]);
|
||||
});
|
||||
};
|
||||
|
||||
EmbedsMany.prototype.build = HasOne.prototype.build = function(targetModelData) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
targetModelData = targetModelData || {};
|
||||
|
||||
this.definition.applyProperties(this.modelInstance, targetModelData);
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
|
||||
var inst = new modelTo(targetModelData);
|
||||
|
||||
if (this.definition.options.prepend) {
|
||||
|
|
|
@ -1464,6 +1464,29 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should build embedded items', function(done) {
|
||||
Person.create({ name: 'Wilma' }, function(err, p) {
|
||||
p.addressList.build({ id: 'home', street: 'Home' });
|
||||
p.addressList.build({ id: 'work', street: 'Work' });
|
||||
p.addresses.should.have.length(2);
|
||||
p.save(function(err, p) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should have embedded items - verify', function(done) {
|
||||
Person.findOne({ where: { name: 'Wilma' } }, function(err, p) {
|
||||
p.name.should.equal('Wilma');
|
||||
p.addresses.should.have.length(2);
|
||||
p.addresses[0].id.should.equal('home');
|
||||
p.addresses[0].street.should.equal('Home');
|
||||
p.addresses[1].id.should.equal('work');
|
||||
p.addresses[1].street.should.equal('Work');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue