JSONStore代码不起作用

阿尼尔·库玛(Anil Kumar)

我正在尝试学习JSONStore,并在此过程中尝试执行一段代码,该代码将首先检查设备中是否已存在特定的JSONStore,并根据结果将执行if-else语句。如果不存在,它将创建一个并在其中添加一些数据。如果jsonStore已经存在,则代码将用新数据替换以前存储的数据。但是,当我尝试执行代码时,我的设备显示html内容一段时间,然后屏幕变黑。当我检查logcat时,没有得到我添加到代码中的任何控制台日志语句。任何人都可以帮助我了解这种行为以及为达到要求可以做什么。

     var JSONStoreCollections = {};
     var collectionName = 'Person';

    function wlCommonInit(){
require([ "layers/core-web-layer", "layers/mobile-ui-layer" ], dojoInit);

     }

    function dojoInit() {
 require([ "dojo/ready", "dojo/parser", "dojox/mobile", "dojo/dom", "dijit/registry",                "dojox/mobile/ScrollableView" ], function(ready) {
 ready(function() {

    if(!(WL.JSONStore.get(collectionName))){
        console.log("i am in if codition");

                   var Data={
                               Name:'name',
                               Age:27
                          };

         JSONStoreCollections[collectionName] = {};
        JSONStoreCollections[collectionName].searchFields = {Name: 'string'};
        WL.JSONStore.init(JSONStoreCollections)
                                    .then(function () {
                                    console.log("store created");
                                    })
                                    .fail(function (errorObject) {
                                    console.log("store creation failed");
                                    });

                       WL.JSONStore.get(collectionName).add(Data)
                       .then(function () {
                        console.log("data added");
                            })
                            .fail(function (errorObject) {
                            console.log("data addition failed");
                            });
                       var query = {Name: 'name'};

                        WL.JSONStore.get(collectionName)
                        .find(query)
                        .then(function (arrayResults) {

                            console.log(arrayResults);
                            WL.Logger.debug(arrayResults);

                        })
                        .fail(function (errorObject) {
                            console.log(errorObject);

                            WL.Logger.debug(errorObject);
                        });             

                    }
               else{
                       var Data1={
                               Name:'name1',
                               Age:30
                          };

                       WL.JSONStore.get(collectionName).replace(Data1)
                       .then(function () {
                        console.log("data replaced");
                            })
                            .fail(function (errorObject) {
                            console.log("data replacement failed");
                            });

                                           var query = {Name: 'name1'};

                        WL.JSONStore.get(collectionName)
                        .find(query)
                        .then(function (arrayResults) {

                            console.log(arrayResults);
                            WL.Logger.debug(arrayResults);

                        })
                        .fail(function (errorObject) {
                            console.log(errorObject);

                            WL.Logger.debug(errorObject);
                        });                                      


               }

    });
});
}
cnandreu
  1. 您需要先进行初始化,否则WL.JSONStore.get(collectionName)将始终返回未定义。如果您从未初始化JSONStore,则无法使用它。
  2. replaceAPI可用于JSONStore文档(具有_id键的对象json)。
  3. 您只需要一个.fail,错误对象就会告诉您错误的来源(errorObject.src)。

请参阅下面未经测试的伪代码,以了解您要执行的操作:

function wlCommonInit () {

var collectionName = 'Person';

var Data = {
 Name: 'name',
 Age: 27
};

var JSONStoreCollections = {};
JSONStoreCollections[collectionName] = {};
JSONStoreCollections[collectionName].searchFields = {Name: 'string'};

WL.JSONStore.init(JSONStoreCollections)

.then(function () {
  WL.Logger.debug('Init done');
  return WL.JSONStore.get(collectionName).findAll();
})

.then(function (res) {
  WL.Logger.debug('Find All returned:', res);

  if (res.length < 1) {

    return WL.JSONStore.get(collectionName).add(Data);

  } else {

    res[0].json = {
      Name:'name1',
      Age:30
    };

    return WL.JSONStore.get(collectionName).replace(res[0]);
  }

})

.then(function () {
  WL.Logger.debug('Add or Replace done');
  return WL.JSONStore.get(collectionName).find({Name: 'name'});
})

.then(function (res) {
  WL.Logger.info('Final Find returned:', res);
})

.fail(function (err) {
  WL.Logger.error(err);
});

}

首次执行时的预期输出:

Init done
Find All returned: []
Add or Replace done
Final Find returned: [{"_id":1,"json":{"Name":"name","Age":27}}] 

除第一次执行外的预期输出:

Init done
Find All returned: [{"_id":1,"json":{"Name":"name","Age":27}}]
Add or Replace done
Final Find returned: [{"_id":1,"json":{"Name":"name1","Age":30}}] 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章