diff --git a/README.md b/README.md index 2c3459f..9edaff3 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ angular.module('myModuleName') * find all persons older than 40 years */ - var myQuery = $indexedDB.queryBuilder().$index('age_idx').$gt(40).$asc.compile(); + var myQuery = $indexedDB.queryBuilder().$index('age_idx').$gt(40).$asc().compile(); myObjectStore.each(myQuery).then(function(cursor){ cursor.key; cursor.value; diff --git a/src/indexeddb.js b/src/indexeddb.js index ebeb768..fcf873e 100644 --- a/src/indexeddb.js +++ b/src/indexeddb.js @@ -203,7 +203,7 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { req.onnotify = function(e) { $rootScope.$apply(function(){ d.notify(e.target.result); - }); + }); } req.onerror = function(e) { $rootScope.$apply(function(){ @@ -252,7 +252,7 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { req.onnotify = function(e) { $rootScope.$apply(function(){ d.notify(e.target.result); - }); + }); } req.onerror = function(e) { $rootScope.$apply(function(){ @@ -423,10 +423,10 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { * * @params {object} options optional query parameters, see defaultQueryOptions * and QueryBuilder for details - * @returns {object} IDBCursor ...wrapped in a promise + * @returns {object} Array of values from store ...wrapped in a promise */ "each": function(options){ - var d = $q.defer(); + var results = [], d = $q.defer(); return this.internalObjectStore(this.storeName, READWRITE).then(function(store){ var req; options = options || defaultQueryOptions; @@ -436,9 +436,16 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { req = store.openCursor(options.keyRange, options.direction); } req.onsuccess = req.onerror = function(e) { - $rootScope.$apply(function(){ - d.resolve(e.target.result); - }); + var cursor = e.target.result; + if(cursor){ + if(!options.filter || options.filter(cursor.value)) + results.push(cursor.value); + cursor.continue(); + } else { + $rootScope.$apply(function(){ + d.resolve(results); + }); + } }; return d.promise; }); @@ -453,7 +460,7 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { * @description utility object to easily create IDBKeyRange for cursor queries */ var QueryBuilder = function() { - this.result = defaultQueryOptions; + this.result = angular.extend({}, defaultQueryOptions); }; QueryBuilder.prototype = { /** @@ -588,6 +595,20 @@ angular.module('xc.indexedDB', []).provider('$indexedDB', function() { this.result.useIndex = indexName; return this; }, + /** + * @ngdoc method + * @name QueryBuilder.$filter + * @function + * + * @description optionally specify an function to determine if the item should be included + * + * @params {function} filter filter function to use + * @returns {object} this QueryBuilder, for chaining params + */ + "$filter": function(filter) { + this.result.filter = filter; + return this; + }, /** * @ngdoc method * @name QueryBuilder.compile