Search code examples
javascriptlocal-storagemethod-chainingjsonstoreibm-mobilefirst

Mobile first - Encrypted Cache Success and Failure Handler


In MobileFirst V6.3 once we call a JSON Store API, Success and failure can be captured using .then() & .fail(). To chain the API calls we can use multiple .then(). Let's say,

WL.JSONStore.startTransaction()
.then(function () {
  var data = [{name: 'carlos'}];
  return WL.JSONStore.get(collectionName).add(data);
})
.then(function () {
  var docs = [{_id: 1, json: {name: 'carlos'}}];
  return WL.JSONStore.get(collectionName).remove(docs);
})
.then(function () {
  return WL.JSONStore.commitTransaction();
})

.fail(function (errorObject) {
  WL.JSONStore.rollbackTransaction()
  .then(function () {
    // Handle rollback success.
  })

  .fail(function () {
    // Handle rollback failure.
  })
});

Since Encrypted Cache API has its own API's callback methods, like below.

WL.EncryptedCache.open(credentials, create_if_none, onCompleteHandler, onErrorHandler);

How to handle Encrypted Cache API chain call's similar to JSON Store[Avoiding callback methods for each API Call's]?

If its not available in out-of-box, is any work around available to achieve the same.

A snippet will be helpful.


Solution

    • The recommendation is to use JSONStore.
    • Chaining callbacks is not supported out of the box.

    The way to do it, is for someone to implement wrappers for the methods that are using callbacks. If you insist on doing that, you'll need to implement something that will look like this:

    function wrapper() {
       var myVar = $.Deferred();
       Wl.EncryptedCache.open(credentials, create_if_none, myVar.resolve, myVar.reject);
       return myVar;
    }
    

    From the user's code:

    wrapper.then(
     function() {success flow...},
     function() {failure flow...}
    );