Search code examples
ibm-mobilefirstjsonstore

First initialization of a JSONStore Collection


So in Worklight, using JSONStore I want to initialize a collection the first time the app is ever loaded.

I want to fill it with a 'status' field with 36 instances of it. On the very first time the app is loaded I want to make all of these set to 0.

After this first initialization the app will update the status values from time to time based on user actions...

How do I initialize all the values at zero just the first time, and not again after that.

Thanks!

(and sorry if this question makes no sense..)


Solution

  • There's a count API you can use to get the number of documents in the collection. If that number is 0, it means it's the first time the collection was initialized, so you can add your 36 instances with status 0 there. For example:

    WL.JSONStore.init(...)
    
    .then(function () {
      return WL.JSONStore.get('collection').count();
    })
    
    .then(function (numOfDocsInCollection) {
    
      if (numOfDocsInCollection < 1) {
        //this code will be reached only the first time the collection has been initialized
      } else {
        //this code will be reached every other time
      }
    
    });