简体   繁体   中英

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..)

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
  }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM