Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 29 additions & 8 deletions src/indexeddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
Expand All @@ -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 = {
/**
Expand Down Expand Up @@ -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
Expand Down