首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSONStore代码不工作

JSONStore代码不工作
EN

Stack Overflow用户
提问于 2014-03-16 12:49:50
回答 1查看 1.1K关注 0票数 1

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

代码语言:javascript
复制
     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);
                        });                                      


               }

    });
});
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-16 14:37:14

  1. 您需要先初始化,否则WL.JSONStore.get(collectionName)总是返回未定义的。如果您从未初始化JSONStore,则不能使用它。
  2. replace API使用JSONStore文档(具有_idjson键的对象)。
  3. 您只需要一个.fail,错误对象就会告诉您错误的来源(errorObject.src)。

有关您想要做的事情,请参阅下面未测试的伪代码:

代码语言:javascript
复制
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);
});

}

第一次执行预期输出:

代码语言:javascript
复制
Init done
Find All returned: []
Add or Replace done
Final Find returned: [{"_id":1,"json":{"Name":"name","Age":27}}] 

预期输出,但第一次执行时除外:

代码语言:javascript
复制
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}}] 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22437044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档