Merge pull request #553 from fabien/fix/save-embed-model

Save parent model of embedded relations
This commit is contained in:
Raymond Feng 2015-04-01 08:50:17 -07:00
commit 1461400f72
2 changed files with 44 additions and 8 deletions

View File

@ -1900,10 +1900,17 @@ EmbedsOne.prototype.create = function (targetModelData, cb) {
var inst = this.callScopeMethod('build', targetModelData);
var updateEmbedded = function() {
modelInstance.updateAttribute(propertyName,
inst, function(err) {
cb(err, err ? null : inst);
});
if (modelInstance.isNewRecord()) {
modelInstance.setAttribute(propertyName, inst);
modelInstance.save(function(err) {
cb(err, err ? null : inst);
});
} else {
modelInstance.updateAttribute(propertyName,
inst, function(err) {
cb(err, err ? null : inst);
});
}
};
if (this.definition.options.persistent) {
@ -2385,10 +2392,17 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
var inst = this.callScopeMethod('build', targetModelData);
var updateEmbedded = function() {
modelInstance.updateAttribute(propertyName,
embeddedList, function(err, modelInst) {
cb(err, err ? null : inst);
});
if (modelInstance.isNewRecord()) {
modelInstance.setAttribute(propertyName, embeddedList);
modelInstance.save(function(err) {
cb(err, err ? null : inst);
});
} else {
modelInstance.updateAttribute(propertyName,
embeddedList, function(err) {
cb(err, err ? null : inst);
});
}
};
if (this.definition.options.persistent) {

View File

@ -2407,6 +2407,17 @@ describe('relations', function () {
});
});
it('should save an unsaved model', function(done) {
var p = new Person({name: 'Fred'});
p.isNewRecord().should.be.true;
p.passportItem.create({name: 'Fredric'}, function(err, passport) {
should.not.exist(err);
p.passport.should.equal(passport);
p.isNewRecord().should.be.false;
done();
});
});
});
describe('embedsOne - persisted model', function () {
@ -2710,6 +2721,17 @@ describe('relations', function () {
done();
});
});
it('should save an unsaved model', function(done) {
var p = new Person({name: 'Fred'});
p.isNewRecord().should.be.true;
p.addressList.create({ street: 'Street 4' }, function(err, address) {
should.not.exist(err);
address.street.should.equal('Street 4');
p.isNewRecord().should.be.false;
done();
});
});
});