2014-09-02 58 views
0

我需要替換IBM Worklight的JSONStore中的多個值。 這樣只保存第一個值。爲什麼?如何保存多個值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

你想建立JSONStore文件的數組,並把它傳遞給replace API。例如:

.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 
}) 

我認爲這發生在JavaScript實現的JSONStore API的,如果是這樣的答案是文檔here的情況下。 JSONStore的JavaScript實現期望代碼被串行調用。在打電話給下一個之前等待一個操作完成。當您多次調用replace而無需等待時,您可以並行調用API而不是串行調用。這不應該是生產環境中的問題(即Android,iOS,WP8和W8)。

+0

謝謝!這樣所有的作品。 – Marco 2014-09-02 15:02:26