【发布时间】:2014-05-20 23:11:57
【问题描述】:
我正在研究 IBM Worklight,并且对 JSONStore 有疑问。如何编写一个函数来删除 JSONStore 集合中的所有文档并保留集合的引用?
换句话说,我想删除文档而不删除集合。我无法在我的应用程序中使用 removeCollection(),因为我无法退出应用程序并再次调用 wlCommonInit()(在 JSONStore 上调用 get 和 init)。
非常感谢您的帮助 安德烈亚
【问题讨论】:
我正在研究 IBM Worklight,并且对 JSONStore 有疑问。如何编写一个函数来删除 JSONStore 集合中的所有文档并保留集合的引用?
换句话说,我想删除文档而不删除集合。我无法在我的应用程序中使用 removeCollection(),因为我无法退出应用程序并再次调用 wlCommonInit()(在 JSONStore 上调用 get 和 init)。
非常感谢您的帮助 安德烈亚
【问题讨论】:
假设 access 是您收藏的访问者,您可以这样做:
access.findAll()
.then(function(result){
if(result.length>0)
{
access.remove(result,{push:false})
}
})
.fail(function(error_msg){
alert(error_msg);
});
但请记住,这不会重置 id(愚蠢的 jsonstore !),因此每次执行此操作时它们都会根据集合的长度移动。
P.S.:根据我的经验,在加密集合的情况下应避免使用 removeCollection API,因为在低性能移动设备上初始化加密集合需要时间...
【讨论】:
目前还没有 API 可以轻松实现这一点。您的选择是:
1.调用remove collection然后初始化你想要清除和重用的特定集合。无需再次致电wlCommonInit。一些伪代码:
var collections = {
people : {...},
orders: {...},
greetings: {...}
};
var options = {...};
WL.JSONStore.get('greetings').removeCollection()
.then(function () {
return WL.JSONStore.init({greetings: collections.greetings}, options);
})
.then(function () {
//re-use the collection here
});
2.使用find API 定位文档,使用remove API 删除它们。有一个example here。
您可以打开功能请求here。
【讨论】: