webcss / angular-indexedDB

An angularjs serviceprovider to utilize indexedDB with angular
149 stars 99 forks source link

each method using callback #27

Closed othermore closed 10 years ago

othermore commented 10 years ago

In the previous version, the "each" method returns a promise returning the cursor. With the cursor, you can perform operations like continue or advance. However, the "onsuccess" property of the cursor can't be changed.

The "each" method receives this onsuccess method and resolves the promise, passing the cursor. The problem is that promises can't and shouldn't be resolved more than once, so it's impossible to access the cursor after the first element.

The submitted change allows to pass a callback to the each function. This way, we use the each in the more usual way, receiving a callback that will receive the objects. This callback could receive the value itself, but I rather pass it the cursor, so it can perform operations such advance().

It makes no sense to use the promise to pass the cursor anymore, so I decided to use it to notify the end of the cursor. A sample code would work as follow:

store.each(function(cursor){
  value = cursor.value;
  //Do whatever you want with value
  cursor.continue();
}).then(function(){
  console.log("Cursor end reached")
});

If you want to advance in the cursor, to jump to a certain item or page, you can do as follow:

var init = true;
store.each(function(cursor){
  if (init)
  {
    init = false;
    cursor.advance(page*elements_by_page);
    return;
  }
  value = cursor.value;
  //Do whatever you want with value
  cursor.continue();
}).then(function(){
  console.log("Cursor end reached")
});
othermore commented 10 years ago

I have added some new features: now the module.debugMode allows to set debug mode to true or false. If defaults to false.

If set to false, the module won't produce any console or alert output.

This parameter can be modified from the app config, with something like:

    $indexedDBProvider.debugMode = true;