简体   繁体   English

首次初始化JSONStore集合

[英]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. 因此,在Worklight中,我想使用JSONStore在首次加载应用程序时初始化一个集合。

I want to fill it with a 'status' field with 36 instances of it. 我想用36个实例的“状态”字段填充它。 On the very first time the app is loaded I want to make all of these set to 0. 第一次加载应用程序时,我要将所有这些设置为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. 您可以使用count API获取集合中文档的数量。 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. 如果该数字为0,则表示这是首次初始化集合,因此您可以在其中添加状态为0的36个实例。 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
  }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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