簡體   English   中英

在循環中向worklight jsonstore添加文檔

[英]adding documents to worklight jsonstore in a loop

我想動態生成列表條目,並同時將它們作為jsonstore文檔添加到我的本地存儲中。

當我這樣做時:

     var j=0;
       while(j<7) {

        /* populating our jsonstore */   
        accessor.add({stuff_to_add})
        .then(function(){})

        /* showing it to the user */
        $('<li>').attr({attributes}).html('html').appendTo('element');   
        j++;

       }

只添加了一個文檔,因為我認為worklight不會自動將添加請求放入隊列中,如果前一個未解析,則取消最后一個,或者沿着這些行取消。

所以,當我這樣做時:

      var j=0;
       while(j<7) {

        /* populating our jsonstore */   
        accessor.add({stuff_to_add})
        .then(function(){

        /* showing it to the user */
        $('<li>').attr({attributes}).html('html').appendTo('element');   
        j++;   })

       }

Mozilla完全崩潰,甚至沒有成功停止腳本我不明白為什么因為它應該只調用add函數多次=(調用時間(accessor.add)/循環時間)應該是有限的。

編輯:實際上,如果我們假設worklight沒有將文檔放入添加隊列,則每次循環循環時都會替換初始添加請求,並且它永遠不會完成,從而解釋崩潰。

編輯2:嘗試使用遞歸函數調用自身,直到j達到7而不是循環

編輯2勝:

var j=0;
       while(j<7) {

        /* creating the ui*/
        $('<li>').attr({attributes}).html('html').appendTo('element');
        j++;

       }

       /* populating jsonstore */

      add_documents(0,stuff_to_add);

其中add_documents(0,stuff_to_add)的定義如下:

add_documents = function(n,stuff_to_add){ 

    if(n<7){
        accessor.add({stuff_to_add})
        .then(function(){alert(n);add_documents(n+1,stuff_to_add);});
    }
    else
    {return true;}
};

add API可以采用JSON對象數組,例如:

var data = [{name: 'carlos'}, {name: 'mike'}];

WL.JSONStore.get('collection').add(data)

.then(function () {

  /*update the UI here*/

  var len = data.length;
  while (len--) {
    console.log(data[len].name);
  }

})

.fail(function (err) {
  /*handle failure*/
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM