Set the relation property correctly

See https://github.com/strongloop/loopback/issues/218
This commit is contained in:
Raymond Feng 2014-03-23 21:07:04 -07:00
parent 82c687737a
commit 0e7acec591
2 changed files with 13 additions and 2 deletions

View File

@ -110,7 +110,11 @@ Inclusion.include = function (objects, include, cb) {
defineCachedRelations(obj); defineCachedRelations(obj);
// Set to null if the owner doesn't exist // Set to null if the owner doesn't exist
obj.__cachedRelations[relationName] = null; obj.__cachedRelations[relationName] = null;
obj[relationName] = null; if(obj === inst) {
obj.__data[relationName] = null;
} else {
obj[relationName] = null;
}
return callback(); return callback();
} }
} }
@ -123,7 +127,12 @@ Inclusion.include = function (objects, include, cb) {
} else { } else {
defineCachedRelations(obj); defineCachedRelations(obj);
obj.__cachedRelations[relationName] = result; obj.__cachedRelations[relationName] = result;
obj[relationName] = result; if(obj === inst) {
obj.__data[relationName] = result;
obj.strict = false;
} else {
obj[relationName] = result;
}
if (subInclude && result) { if (subInclude && result) {
var subItems = relation.multiple ? result : [result]; var subItems = relation.multiple ? result : [result];

View File

@ -70,6 +70,7 @@ describe('include', function () {
should.exist(user); should.exist(user);
user.id.should.equal(p.ownerId); user.id.should.equal(p.ownerId);
user.__cachedRelations.should.have.property('posts'); user.__cachedRelations.should.have.property('posts');
user.should.have.property('posts');
user.__cachedRelations.posts.forEach(function (pp) { user.__cachedRelations.posts.forEach(function (pp) {
pp.userId.should.equal(user.id); pp.userId.should.equal(user.id);
}); });
@ -97,6 +98,7 @@ describe('include', function () {
user.__cachedRelations.should.have.property('posts'); user.__cachedRelations.should.have.property('posts');
user.__cachedRelations.posts.forEach(function (pp) { user.__cachedRelations.posts.forEach(function (pp) {
pp.userId.should.equal(user.id); pp.userId.should.equal(user.id);
pp.should.have.property('author');
pp.__cachedRelations.should.have.property('author'); pp.__cachedRelations.should.have.property('author');
var author = pp.__cachedRelations.author; var author = pp.__cachedRelations.author;
author.id.should.equal(user.id); author.id.should.equal(user.id);