【问题标题】:Remove all documents in a JSONStore collection (without using removeCollection() )删除 JSONStore 集合中的所有文档(不使用 removeCollection() )
【发布时间】:2014-05-20 23:11:57
【问题描述】:

我正在研究 IBM Worklight,并且对 JSONStore 有疑问。如何编写一个函数来删除 JSONStore 集合中的所有文档并保留集合的引用?

换句话说,我想删除文档而不删除集合。我无法在我的应用程序中使用 removeCollection(),因为我无法退出应用程序并再次调用 wlCommonInit()(在 JSONStore 上调用 get 和 init)。

非常感谢您的帮助 安德烈亚

【问题讨论】:

    标签: ibm-mobilefirst jsonstore


    【解决方案1】:

    假设 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,因为在低性能移动设备上初始化加密集合需要时间...

    【讨论】:

      【解决方案2】:

      目前还没有 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

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 1970-01-01
        • 2020-09-08
        • 2018-08-08
        • 2018-11-01
        • 2014-02-06
        • 1970-01-01
        相关资源
        最近更新 更多