Javascript 在循环中将文档添加到worklight jsonstore

Javascript 在循环中将文档添加到worklight jsonstore,javascript,ibm-mobilefirst,jsonstore,Javascript,Ibm Mobilefirst,Jsonstore,我希望动态生成列表条目,同时将它们作为jsonstore文档添加到本地存储中 当我这样做的时候: var j=0; while(j<7) { /* populating our jsonstore */ accessor.add({stuff_to_add}) .then(function(){}) /* showing it to the user */ $('<li&

我希望动态生成列表条目,同时将它们作为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++;

       }
      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++;   })

       }
var j=0;
而(j编辑两个胜利:

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);
var j=0;

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*/
});

哦,好吧,我想这样写比较容易。我不知道这是否等同于我的解决方案。请注意,仍然没有解决方案来生成一个组件并将其添加到同一个循环中的存储中。传递数组应该比在您的答案中递归调用
add\u documents
函数具有更少的开销。也就是说,最终结果是t应该是相同的。您可以随时添加以查看jsonstore.js代码的工作方式。
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*/
});