From abf402b8c54cb6993aa310e00dbe918b884af629 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 10 Nov 2015 16:00:26 -0800 Subject: [PATCH] Terminate cursor cleanly when using limit and index Add db adapter support for clean cursor termination when passing options.index and options.limit together. Previously this would cause a crash because options.conditions was undefined. // FREEBIE --- .../backbone-indexeddb.js | 12 ++++++++++-- js/components.js | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js b/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js index ac77b6ce..2a7e93b9 100644 --- a/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js +++ b/components/indexeddb-backbonejs-adapter/backbone-indexeddb.js @@ -476,8 +476,16 @@ // Cursor is not over yet. if (options.limit && processed >= options.limit) { // Yet, we have processed enough elements. So, let's just skip. - if (bounds && options.conditions[index.keyPath]) { - cursor.continue(options.conditions[index.keyPath][1] + 1); /* We need to 'terminate' the cursor cleany, by moving to the end */ + if (bounds) { + if (options.conditions && options.conditions[index.keyPath]) { + cursor.continue(options.conditions[index.keyPath][1] + 1); /* We need to 'terminate' the cursor cleany, by moving to the end */ + } else if (options.index && (options.index.upper || options.index.lower)) { + if (typeof options.index.order === 'string' && options.index.order.toLowerCase() === 'desc') { + cursor.continue(options.index.lower); + } else { + cursor.continue(options.index.upper); + } + } } else { cursor.continue(); /* We need to 'terminate' the cursor cleany, by moving to the end */ } diff --git a/js/components.js b/js/components.js index ebc12ea8..f2188b02 100644 --- a/js/components.js +++ b/js/components.js @@ -21834,8 +21834,16 @@ return jQuery; // Cursor is not over yet. if (options.limit && processed >= options.limit) { // Yet, we have processed enough elements. So, let's just skip. - if (bounds && options.conditions[index.keyPath]) { - cursor.continue(options.conditions[index.keyPath][1] + 1); /* We need to 'terminate' the cursor cleany, by moving to the end */ + if (bounds) { + if (options.conditions && options.conditions[index.keyPath]) { + cursor.continue(options.conditions[index.keyPath][1] + 1); /* We need to 'terminate' the cursor cleany, by moving to the end */ + } else if (options.index && (options.index.upper || options.index.lower)) { + if (typeof options.index.order === 'string' && options.index.order.toLowerCase() === 'desc') { + cursor.continue(options.index.lower); + } else { + cursor.continue(options.index.upper); + } + } } else { cursor.continue(); /* We need to 'terminate' the cursor cleany, by moving to the end */ }