代码之家  ›  专栏  ›  技术社区  ›  Marco

如何保存多个值JSONStore

  •  0
  • Marco  · 技术社区  · 12 年前

    我需要替换IBM Worklight的JSONStore中的多个值。 这样只保存第一个值。为什么?

     .then(function() {                             
       for (var index = 0; index < elencoSpese.length; index++) {                                          
           var spesa = elencoSpese[index];                              
           var spesaReplace = {_id: spesa.id, json: spesa};                                 
           spesa.id_nota_spesa = idNotaSpesa;                               
           spesa.checked = true;                            
           WL.JSONStore.get(COLLECTION_NAME_SPESE).replace(spesaReplace);
        }
      })
    
    1 回复  |  直到 12 年前
        1
  •  1
  •   cnandreu    12 年前

    您需要构建一个JSONStore文档数组,并将其传递给 replace 应用程序编程接口。例如:

    .then(function() {
    
        var replacementsArray = [];
    
        for (var index = 0; index < elencoSpese.length; index++) {
            var spesa = elencoSpese[index];
            var spesaReplace = {_id: spesa.id, json: spesa};
            spesa.id_nota_spesa = idNotaSpesa;
            spesa.checked = true;
            replacementsArray.push(spesaReplace);
        }
    
        return WL.JSONStore.get(COLLECTION_NAME_SPESE).replace(replacementsArray);
    
    })
    .then(function (numOfDocsReplaced) {
        // numOfDocsReplaced should equal elencoSpese.length
    })
    

    我假设这发生在JSONStore API的JavaScript实现中,如果是这种情况,答案在文档中 here . JSONStore的JavaScript实现期望代码被串行调用。等待一个操作完成后再调用下一个操作。 当您拨打 代替 无需等待多次,您就可以并行而不是串行地调用API。这在生产环境(即Android、iOS、WP8和W8)中不应成为问题。

    推荐文章