From 3f8a00c99091cc77c2cdc4804ae4690570cfb76e Mon Sep 17 00:00:00 2001 From: AlikSend Date: Thu, 28 May 2015 16:29:05 +0300 Subject: [PATCH 1/2] added the ability to use scope and functions as the skip and limit options --- lib/Model/filter.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/Model/filter.js b/lib/Model/filter.js index a1412565c..2b15fda77 100644 --- a/lib/Model/filter.js +++ b/lib/Model/filter.js @@ -162,10 +162,28 @@ Filter.prototype.sort = function(fn) { Filter.prototype._slice = function(results) { if (this.skip == null && this.limit == null) return results; - var begin = this.skip || 0; + var skip; + if (this.skip instanceof Model) { + skip = this.skip.get(); + } else if (typeof this.skip === 'function') { + skip = this.skip(); + } else { + skip = this.skip; + } + var begin = skip || 0; // A limit of zero is equivalent to setting no limit var end; - if (this.limit) end = begin + this.limit; + if (this.limit) { + var limit; + if (this.limit instanceof Model) { + limit = this.limit.get(); + } else if (typeof this.limit === 'function') { + limit = this.limit(); + } else { + limit = this.limit; + } + end = begin + limit; + } return results.slice(begin, end); }; From a6ec0ebe2d7935af4b676b607b83de946b9a8efe Mon Sep 17 00:00:00 2001 From: AlikSend Date: Fri, 29 May 2015 13:59:20 +0300 Subject: [PATCH 2/2] correct: explicit cast scope.get() to int --- lib/Model/filter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Model/filter.js b/lib/Model/filter.js index 2b15fda77..314b8334e 100644 --- a/lib/Model/filter.js +++ b/lib/Model/filter.js @@ -164,7 +164,7 @@ Filter.prototype._slice = function(results) { if (this.skip == null && this.limit == null) return results; var skip; if (this.skip instanceof Model) { - skip = this.skip.get(); + skip = parseInt(this.skip.get()); } else if (typeof this.skip === 'function') { skip = this.skip(); } else { @@ -176,7 +176,7 @@ Filter.prototype._slice = function(results) { if (this.limit) { var limit; if (this.limit instanceof Model) { - limit = this.limit.get(); + limit = parseInt(this.limit.get()); } else if (typeof this.limit === 'function') { limit = this.limit(); } else {