Merge pull request #553 from fabien/fix/save-embed-model
Save parent model of embedded relations
This commit is contained in:
commit
1461400f72
|
@ -1900,10 +1900,17 @@ EmbedsOne.prototype.create = function (targetModelData, cb) {
|
|||
var inst = this.callScopeMethod('build', targetModelData);
|
||||
|
||||
var updateEmbedded = function() {
|
||||
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) {
|
||||
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) {
|
||||
|
|
|
@ -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 () {
|
||||
|
@ -2711,6 +2722,17 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('embedsMany - numeric ids + forceId', function () {
|
||||
|
|
Loading…
Reference in New Issue