【问题标题】:Save multiple times at almost the same time in workligth JSONStore在 workligth JSONStore 中几乎同时保存多次
【发布时间】:2014-11-20 19:57:16
【问题描述】:

我想使用 for 循环添加到 JSONStore

所以我在 for 中调用了长度超过 500 的 saveToJSON() 函数,但它没有添加,并且在控制台中它显示成功,但是当我查看 jsonstore 时什么都没有,以及循环次数我调用添加到 jsonstore 在控制台中的红色气泡中。

function saveToJSON(object) {

var data ={

    title :     object.title, 
    subtitle:   object.subtitle,         

  };

var options = {}; //default
WL.JSONStore.get('MyDataCollection').add(data, options)
  .then(function () {
    //handle success
    console.log("add JSONStore success");

    })
  .fail(function (errorObject) {
    //handle failure
    console.log("add JSONStore failure");

});

}

【问题讨论】:

    标签: javascript html ibm-mobilefirst jsonstore


    【解决方案1】:

    不建议在 JSONStore 中执行并行操作。 JSONtore 旨在异步工作。您可以使用 for 循环连续运行 JSONStore 操作。但是,您的示例没有显示 for 循环。您是否尝试过使用较小的迭代进行 for 循环?可能是 2 而不是 500。

    【讨论】:

      【解决方案2】:

      尝试使用您要添加的数据创建一个数组,然后将其传递给 JSONStore 的添加 API。请记住在调用添加 API 之前确保 WL.JSONStore.init 已成功完成。

      示例伪代码:

      //This is the data you want to add, you probably get this from a network call
      var someData = [{title: 'hello'}, {title: 'world'}];
      
      //This is an array that you will pass to JSONStore's add API
      var someArray = [];
      
      //Populate the array with data you want to pass to JSONStore's add API
      for (var i = 0; i < someData.length; i++) {
          someArray.push(someData[i]);
      }
      
      //Add data inside someArray to the collection called: MyDataCollection
      WL.JSONStore.get('MyDataCollection').add(someArray)
      .then(function () {
      
          //Do a find all operation on the collection called: MyDataCollection
          return WL.JSONStore.get('MyDataCollection').findAll();
      })
      .then(function (res) {
      
          //Print all the data inside the collection called: MyDataCollection
          console.log(JSON.stringify(res));
      });
      
      //You may want to add .fail(function(){...}) to handle errors.
      

      【讨论】:

        猜你喜欢
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-12-29
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        • 2015-12-10
        • 1970-01-01
        相关资源
        最近更新 更多