| 235 | }; |
| 236 | |
| 237 | paginate(pageNo, totalItemsInPage, callback) { |
| 238 | |
| 239 | if (!CB.appId) { |
| 240 | throw "CB.appId is null."; |
| 241 | } |
| 242 | if (!this.tableName) { |
| 243 | throw "TableName is null."; |
| 244 | } |
| 245 | var def; |
| 246 | var callback; |
| 247 | if (typeof callback === 'object' && typeof callback.success === 'function') { |
| 248 | callback = callback; |
| 249 | } |
| 250 | if (!callback) { |
| 251 | def = new CB.Promise(); |
| 252 | } |
| 253 | |
| 254 | if (pageNo && typeof pageNo === 'object' && typeof pageNo.success === 'function') { |
| 255 | callback = pageNo; |
| 256 | pageNo = null; |
| 257 | } |
| 258 | if (totalItemsInPage && typeof totalItemsInPage === 'object' && typeof totalItemsInPage.success === 'function') { |
| 259 | callback = totalItemsInPage; |
| 260 | totalItemsInPage = null; |
| 261 | } |
| 262 | |
| 263 | if (pageNo && typeof pageNo === 'number' && pageNo > 0) { |
| 264 | if (typeof totalItemsInPage === 'number' && totalItemsInPage > 0) { |
| 265 | var skip = (pageNo * totalItemsInPage) - totalItemsInPage; |
| 266 | this.setSkip(skip); |
| 267 | this.setLimit(totalItemsInPage); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if (totalItemsInPage && typeof totalItemsInPage === 'number' && totalItemsInPage > 0) { |
| 272 | this.setLimit(totalItemsInPage); |
| 273 | } |
| 274 | var thisObj = this; |
| 275 | |
| 276 | var promises = []; |
| 277 | promises.push(this.find()); |
| 278 | |
| 279 | var countQuery = Object.create(this); |
| 280 | countQuery.setSkip(0); |
| 281 | countQuery.setLimit(99999999); |
| 282 | |
| 283 | promises.push(countQuery.count()); |
| 284 | |
| 285 | CB.Promise.all(promises).then(function(list) { |
| 286 | var objectsList = null; |
| 287 | var count = null; |
| 288 | var totalPages = 0; |
| 289 | |
| 290 | if (list && list.length > 0) { |
| 291 | objectsList = list[0]; |
| 292 | count = list[1]; |
| 293 | if (!count) { |
| 294 | count = 0; |