[英]IBM Worklight - How to remove JSONStore resources from the application
[英]IBM Worklight JSONStore | Remove Document from Collection and erase it from memory
在添加新文檔之前,我一直試圖從收集和內存中刪除所有文檔。 我通過以下方法嘗試了
1。
WL.JSONStore.get("users").findAll().then(function (arrayResults){
var options={push:true};
if(arrayResults.length>0){
for(var i=1;i<=arrayResults.length;i++){
WL.JSONStore.get("users").remove(i,options);
}
}
});
2。
WL.JSONStore.get("users").findAll().then(function (arrayResults){
var options={};
if(arrayResults.length>0){
for(var i=1;i<=arrayResults.length;i++){
WL.JSONStore.get("users").erase(i,options);
}
}
});
但是沒有成功。 它通過執行findAll
顯示所有文檔
您可以使用removeCollection API調用來刪除集合中的所有文檔,如果要再次使用它,則必須重新初始化集合。
或者,嘗試按照以下示例操作:
function wlCommonInit () {
WL.JSONStore.init({
users: {
name: 'string'
}
})
.then(function () {
return WL.JSONStore.get('users').add([{name: 'hello'}, {name: 'world'}]);
})
.then(function () {
return WL.JSONStore.get('users').findAll();
})
.then(function (res){
alert('Before - Docs inside:' + JSON.stringify(res));
var ids = res.map(function(current){ return current._id })
var promises = [];
while (ids.length) {
promises.push(WL.JSONStore.get('users').remove(ids.pop()));
}
return WLJQ.when.apply(null, promises);
})
.then(function () {
return WL.JSONStore.get('users').findAll();
})
.then(function (res) {
alert('After - Docs inside:' + JSON.stringify(res));
})
.fail(function (err) {
alert('Failed:' + err.toString());
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.