簡體   English   中英

幾乎同時在workligth JSONStore中保存多次

[英]Save multiple times at almost the same time in workligth JSONStore

我想通過使用for周期添加到JSONStore

所以我在for內部調用了saveToJSON()函數,長度超過500,但是它沒有添加,並且在控制台中顯示成功,但是當我在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");

});

}

確實不建議在JSONStore中執行並行操作。 JSONtore設計為異步工作。 您可以使用for循環來串行運行JSONStore操作。 但是,您的示例未顯示for循環。 您是否嘗試過使用較小的迭代進行for循環? 也許2而不是500。

嘗試使用要添加的數據創建一個數組,然后將其傳遞給JSONStore的add API。 請記住在調用add 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM