From d27b2eb25fab85cd1988a7f5f8f793faf2589b09 Mon Sep 17 00:00:00 2001 From: mamboer Date: Wed, 13 May 2015 13:18:50 +0800 Subject: [PATCH 1/3] fix issue #587 --- lib/scope.js | 10 ++++++++++ test/relations.test.js | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/scope.js b/lib/scope.js index c0b2a56d..82a79b52 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -161,6 +161,16 @@ function defineScope(cls, targetClass, name, params, methods, options) { // see https://github.com/strongloop/loopback/issues/1076 if (f._scope.collect && condOrRefresh !== null && typeof condOrRefresh === 'object') { + //extract the paging filters to the through model + if( typeof(condOrRefresh.limit) !== 'undefined' ){ + f._scope.limit = condOrRefresh.limit; + } + if( typeof(condOrRefresh.skip) !== 'undefined' ){ + f._scope.skip = condOrRefresh.skip; + } + delete condOrRefresh.limit; + delete condOrRefresh.skip; + // Adjust the include so that the condition will be applied to // the target model f._scope.include = { diff --git a/test/relations.test.js b/test/relations.test.js index 8168f825..c848b188 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -718,6 +718,27 @@ describe('relations', function () { } }); + it('should fetch scoped instances with paging filters', function (done) { + Physician.create(function (err, physician) { + physician.patients.create({name: 'a'}, function () { + physician.patients.create({name: 'z'}, function () { + physician.patients.create({name: 'c'}, function () { + verify(physician); + }); + }); + }); + }); + function verify(physician) { + physician.patients({ limit:1, skip:1 },function (err, ch) { + should.not.exist(err); + should.exist(ch); + ch.should.have.lengthOf(1); + ch[0].name.should.eql('z'); + done(); + }); + } + }); + it('should find scoped record', function (done) { var id; Physician.create(function (err, physician) { From 5ff3798e8e21ad84563991cbdf1336a90f6fce94 Mon Sep 17 00:00:00 2001 From: mamboer Date: Thu, 14 May 2015 09:55:54 +0800 Subject: [PATCH 2/3] enhancement on #588 --- lib/scope.js | 15 ++++++--------- test/relations.test.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 82a79b52..6df25203 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -162,15 +162,12 @@ function defineScope(cls, targetClass, name, params, methods, options) { if (f._scope.collect && condOrRefresh !== null && typeof condOrRefresh === 'object') { //extract the paging filters to the through model - if( typeof(condOrRefresh.limit) !== 'undefined' ){ - f._scope.limit = condOrRefresh.limit; - } - if( typeof(condOrRefresh.skip) !== 'undefined' ){ - f._scope.skip = condOrRefresh.skip; - } - delete condOrRefresh.limit; - delete condOrRefresh.skip; - + ['limit','offset','skip','order'].forEach(function( pagerFilter ){ + if( typeof( condOrRefresh[ pagerFilter ] ) !== 'undefined' ){ + f._scope[ pagerFilter ] = condOrRefresh[ pagerFilter ]; + delete condOrRefresh[ pagerFilter ]; + } + }); // Adjust the include so that the condition will be applied to // the target model f._scope.include = { diff --git a/test/relations.test.js b/test/relations.test.js index c848b188..4942de6f 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -729,12 +729,27 @@ describe('relations', function () { }); }); function verify(physician) { + //limit plus skip physician.patients({ limit:1, skip:1 },function (err, ch) { should.not.exist(err); should.exist(ch); ch.should.have.lengthOf(1); ch[0].name.should.eql('z'); - done(); + //offset plus skip + physician.patients({ limit:1, offset:1 },function (err1, ch1) { + should.not.exist(err1); + should.exist(ch1); + ch1.should.have.lengthOf(1); + ch1[0].name.should.eql('z'); + //order + physician.patients({ order: "patientId DESC" },function (err2, ch2) { + should.not.exist(err2); + should.exist(ch2); + ch2.should.have.lengthOf(3); + ch2[0].name.should.eql('c'); + done(); + }); + }); }); } }); From 018022e84baf9b92bb4df135ad5acf32726c7ca2 Mon Sep 17 00:00:00 2001 From: mamboer Date: Tue, 26 May 2015 12:33:56 +0800 Subject: [PATCH 3/3] adapt coding style @bajtos --- lib/scope.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index 24f23784..09ce1305 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -193,10 +193,10 @@ function defineScope(cls, targetClass, name, params, methods, options) { if (f._scope.collect && condOrRefresh !== null && typeof condOrRefresh === 'object') { //extract the paging filters to the through model - ['limit','offset','skip','order'].forEach(function( pagerFilter ){ - if( typeof( condOrRefresh[ pagerFilter ] ) !== 'undefined' ){ - f._scope[ pagerFilter ] = condOrRefresh[ pagerFilter ]; - delete condOrRefresh[ pagerFilter ]; + ['limit','offset','skip','order'].forEach(function(pagerFilter){ + if(typeof(condOrRefresh[pagerFilter]) !== 'undefined'){ + f._scope[pagerFilter] = condOrRefresh[pagerFilter]; + delete condOrRefresh[pagerFilter]; } }); // Adjust the include so that the condition will be applied to