Merge branch 'feature/embeds-one' of github.com:fabien/loopback-datasource-juggler into fabien-feature/embeds-one
This commit is contained in:
commit
f5839fb6ae
|
@ -1626,6 +1626,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;
|
||||
|
@ -1706,12 +1707,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) {
|
||||
|
|
|
@ -1429,6 +1429,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