我想通过使用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");
});
}
最佳答案
尝试使用要添加的数据创建一个数组,然后将其传递给JSONStore的add 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.
关于javascript - 几乎同时在workligth JSONStore中保存多次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26048829/