Merge pull request #360 from clarkorz/fix/embedsOne-errors
fix embedsOne error when embed instance is undefined or null
This commit is contained in:
commit
d92905d53e
|
@ -1753,7 +1753,7 @@ RelationDefinition.embedsOne = function (modelFrom, modelTo, params) {
|
|||
configurable: true,
|
||||
get: function() {
|
||||
var relation = new EmbedsOne(definition, this);
|
||||
var relationMethod = relation.related.bind(relation)
|
||||
var relationMethod = relation.related.bind(relation);
|
||||
relationMethod.create = relation.create.bind(relation);
|
||||
relationMethod.build = relation.build.bind(relation);
|
||||
relationMethod.update = relation.update.bind(relation);
|
||||
|
@ -1797,7 +1797,10 @@ EmbedsOne.prototype.related = function (refresh, params) {
|
|||
}
|
||||
|
||||
var embeddedInstance = modelInstance[propertyName];
|
||||
|
||||
if (embeddedInstance) {
|
||||
embeddedInstance.__persisted = true;
|
||||
}
|
||||
|
||||
if (typeof params === 'function') { // acts as async getter
|
||||
var cb = params;
|
||||
|
|
|
@ -2011,6 +2011,7 @@ describe('relations', function () {
|
|||
describe('embedsOne', function () {
|
||||
|
||||
var person;
|
||||
var Passport;
|
||||
var Other;
|
||||
|
||||
before(function () {
|
||||
|
@ -2021,6 +2022,7 @@ describe('relations', function () {
|
|||
{name:{type:'string', required: true}},
|
||||
{idInjection: false}
|
||||
);
|
||||
Address = tmp.define('Address', { street: String }, { idInjection: false });
|
||||
Other = db.define('Other', {name: String});
|
||||
});
|
||||
|
||||
|
@ -2028,6 +2030,7 @@ describe('relations', function () {
|
|||
Person.embedsOne(Passport, {
|
||||
default: {name: 'Anonymous'} // a bit contrived
|
||||
});
|
||||
Person.embedsOne(Address); // all by default
|
||||
db.automigrate(done);
|
||||
});
|
||||
|
||||
|
@ -2040,6 +2043,19 @@ describe('relations', function () {
|
|||
p.passportItem.destroy.should.be.a.function;
|
||||
});
|
||||
|
||||
it('should behave properly without default or being set', function (done) {
|
||||
var p = new Person();
|
||||
should.not.exist(p.address);
|
||||
var a = p.addressItem();
|
||||
should.not.exist(a);
|
||||
Person.create({}, function (err, p) {
|
||||
should.not.exist(p.address);
|
||||
var a = p.addressItem();
|
||||
should.not.exist(a);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an instance with default values', function() {
|
||||
var p = new Person();
|
||||
p.passport.toObject().should.eql({name: 'Anonymous'});
|
||||
|
|
Loading…
Reference in New Issue