JSONStore代码无法正常工作

时间:2014-03-16 12:49:50

标签: android ibm-mobilefirst jsonstore

我正在尝试学习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);
                        });                                      


               }

    });
});
}

1 个答案:

答案 0 :(得分:3)

  1. 您需要先进行初始化,否则WL.JSONStore.get(collectionName)将始终返回undefined。如果您从未初始化JSONStore,则无法使用它。
  2. replace API适用于JSONStore文档(包含_idjson键的对象。)
  3. 您只需要一个.fail,错误对象会告诉您错误的来源(errorObject.src)。
  4. 请参阅下面未经测试的伪代码,了解您要执行的操作:

    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}}]