diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 3a5b7371..42353297 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -1711,6 +1711,7 @@ RelationDefinition.embedsOne = function (modelFrom, modelTo, params) { relationMethod.build = relation.build.bind(relation); relationMethod.update = relation.update.bind(relation); relationMethod.destroy = relation.destroy.bind(relation); + relationMethod.value = relation.embeddedValue.bind(relation); relationMethod._targetClass = definition.modelTo.modelName; return relationMethod; } @@ -1759,6 +1760,11 @@ EmbedsOne.prototype.related = function (refresh, params) { } }; +EmbedsOne.prototype.embeddedValue = function(modelInstance) { + modelInstance = modelInstance || this.modelInstance; + return modelInstance[this.definition.keyFrom]; +}; + EmbedsOne.prototype.create = function (targetModelData, cb) { var modelTo = this.definition.modelTo; var propertyName = this.definition.keyFrom; @@ -1920,7 +1926,8 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params) get: scopeMethod(definition, 'get'), set: scopeMethod(definition, 'set'), unset: scopeMethod(definition, 'unset'), - at: scopeMethod(definition, 'at') + at: scopeMethod(definition, 'at'), + value: scopeMethod(definition, 'embeddedValue') }; var findByIdFunc = scopeMethods.findById; @@ -2000,7 +2007,8 @@ EmbedsMany.prototype.prepareEmbeddedInstance = function(inst) { } }; -EmbedsMany.prototype.embeddedList = function(modelInstance) { +EmbedsMany.prototype.embeddedList = + EmbedsMany.prototype.embeddedValue = function(modelInstance) { modelInstance = modelInstance || this.modelInstance; var embeddedList = modelInstance[this.definition.keyFrom] || []; embeddedList.forEach(this.prepareEmbeddedInstance.bind(this)); @@ -2028,7 +2036,7 @@ EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb var params = mergeQuery(actualCond, scopeParams); - if (params.where) { // TODO [fabien] Support order/sorting + if (params.where && Object.keys(params.where).length > 0) { // TODO [fabien] Support order/sorting embeddedList = embeddedList ? embeddedList.filter(applyFilter(params)) : embeddedList; } diff --git a/lib/scope.js b/lib/scope.js index 3e2fab15..de6cfb8a 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -150,7 +150,11 @@ function defineScope(cls, targetClass, name, params, methods, options) { var f = function(condOrRefresh, cb) { if (arguments.length === 0) { - return self.__cachedRelations && self.__cachedRelations[name]; + if (typeof f.value === 'function') { + return f.value(self); + } else if (self.__cachedRelations) { + return self.__cachedRelations[name]; + } } else if (arguments.length === 1) { return definition.related(self, f._scope, condOrRefresh); } else { diff --git a/test/relations.test.js b/test/relations.test.js index 09b5b123..abc13d99 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -1775,6 +1775,7 @@ describe('relations', function () { passport.toObject().should.eql({name: 'Fredric'}); passport.should.be.an.instanceOf(Passport); passport.should.equal(p.passport); + passport.should.equal(p.passportItem.value()); done(); }); }); @@ -1910,6 +1911,11 @@ describe('relations', function () { Person.findOne(function(err, p) { p.addressList(function(err, addresses) { should.not.exist(err); + + var list = p.addressList(); + list.should.equal(addresses); + list.should.equal(p.addresses); + addresses.should.have.length(2); addresses[0].id.should.eql(address1.id); addresses[0].street.should.equal('Street 1');