Implement update() on embedsOne scope
This commit is contained in:
parent
0290109cdb
commit
1d5079d811
|
@ -1625,6 +1625,7 @@ RelationDefinition.embedsOne = function (modelFrom, modelTo, params) {
|
|||
var relationMethod = relation.related.bind(relation)
|
||||
relationMethod.create = relation.create.bind(relation);
|
||||
relationMethod.build = relation.build.bind(relation);
|
||||
relationMethod.update = relation.update.bind(relation);
|
||||
relationMethod.destroy = relation.destroy.bind(relation);
|
||||
relationMethod._targetClass = definition.modelTo.modelName;
|
||||
return relationMethod;
|
||||
|
@ -1705,12 +1706,39 @@ EmbedsOne.prototype.create = function (targetModelData, cb) {
|
|||
EmbedsOne.prototype.build = function (targetModelData) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var modelInstance = this.modelInstance;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
|
||||
targetModelData = targetModelData || {};
|
||||
|
||||
this.definition.applyProperties(modelInstance, targetModelData);
|
||||
|
||||
return new modelTo(targetModelData);
|
||||
var embeddedInstance = new modelTo(targetModelData);
|
||||
modelInstance[propertyName] = embeddedInstance;
|
||||
|
||||
return embeddedInstance;
|
||||
};
|
||||
|
||||
EmbedsOne.prototype.update = function (targetModelData, cb) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var modelInstance = this.modelInstance;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
|
||||
var isInst = targetModelData instanceof ModelBaseClass;
|
||||
var data = isInst ? targetModelData.toObject() : targetModelData;
|
||||
|
||||
var embeddedInstance = modelInstance[propertyName];
|
||||
if (embeddedInstance instanceof modelTo) {
|
||||
embeddedInstance.setAttributes(data);
|
||||
if (typeof cb === 'function') {
|
||||
modelInstance.save(function(err, inst) {
|
||||
cb(err, inst ? inst[propertyName] : embeddedInstance);
|
||||
});
|
||||
}
|
||||
} else if (!embeddedInstance && cb) {
|
||||
this.create(data, db);
|
||||
} else if (!embeddedInstance) {
|
||||
this.build(data);
|
||||
}
|
||||
};
|
||||
|
||||
EmbedsOne.prototype.destroy = function (cb) {
|
||||
|
|
|
@ -1426,6 +1426,28 @@ describe('relations', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('should update an embedded item on scope', function(done) {
|
||||
Person.findById(personId, function(err, p) {
|
||||
p.passportItem.update({name: 'Freddy'}, function(err, passport) {
|
||||
should.not.exist(err);
|
||||
var passport = p.passportItem();
|
||||
passport.toObject().should.eql({name: 'Freddy'});
|
||||
passport.should.be.an.instanceOf(Passport);
|
||||
passport.should.equal(p.passport);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should get an embedded item on scope - verify', function(done) {
|
||||
Person.findById(personId, function(err, p) {
|
||||
should.not.exist(err);
|
||||
var passport = p.passportItem();
|
||||
passport.toObject().should.eql({name: 'Freddy'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should destroy an embedded item on scope', function(done) {
|
||||
Person.findById(personId, function(err, p) {
|
||||
p.passportItem.destroy(function(err) {
|
||||
|
|
Loading…
Reference in New Issue