[英]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.