【问题标题】:IBM Worklight JSONStore - Add and get dataIBM Worklight JSONStore - 添加和获取数据
【发布时间】:2014-08-07 00:42:15
【问题描述】:

我正在使用 worlight JSONstore。我是新手。我尝试搜索阅读所有文档但没有得到太多想法。

我有一个登录页面,我从中获取了一些 json 数据,我想使用 jsonstore 存储这些数据。然后得到它。

我做了 jsonstore 适配器。

Json-Store-Impl.js

function getJsonStores(custData) {
var data = custData;
return data;
      //custdata is json 
}

 function addJsonStore(param1) {

var input = {
    method : 'put',
    returnedContentType : 'json',
    path : 'userInputRequired'
};


return WL.Server.invokeHttp(input);
}


function updateJsonStore(param1) {

var input = {
    method : 'post',
    returnedContentType : 'json',
    path : 'userInputRequired'
};


return WL.Server.invokeHttp(input);
}


function deleteJsonStore(param1) {


var input = {
    method : 'delete',
    returnedContentType : 'json',
    path : 'userInputRequired'
};


return WL.Server.invokeHttp(input);
}

之后我创建了一个本地 JSON 存储。

famlCollection.js

;(function () {

WL.JSONStore.init({
    faml : {
        searchFields: {"response.mci.txnid":"string","response.mci.scrnseqnbr":"string","response.loginUser":"string","request.fldWebServerId":"string","response.fldRsaImageHeight":"string","request.fldRequestId":"string","request.fldTxnId":"string","response.fldDeviceTokenFSO":"string","response.fldRsaCollectionRequired":"string","response.datlastsuccesslogin":"string","response.fldRsaUserPhrase":"string","response.fldRsaAuthTxnId":"string","response.rc.returncode":"string","response.datcurrentlogin":"string","response.mci.deviceid":"string","response.customername":"string","request.fldDeviceId":"string","response.fldRsaUserStatus":"string","request.fldScrnSeqNbr":"string","response.fldRsaImageWidth":"string","request.fldLangId":"string","response.fldTptCustomer":"string","response.encflag":"string","response.rc.errorcode":"string","response.fldRsaImagePath":"string","response.mci.appid":"string","response.mci.requestid":"string","response.rc.errormessage":"string","response.mci.appserverid":"string","response.fldRsaCollectionType":"string","request.fldAppId":"string","response.fldRsaImageId":"string","request.fldLoginUserId":"string","response.mci.sessionid":"string","response.mci.langid":"string","response.mci.remoteaddress":"string","request.fldAppServerId":"string","response.mci.webserverid":"string","response.fldRsaImageText":"string","response.fldRsaEnrollRequired":"string","response.fldRsaActivityFlag":"string"},
        adapter : {
            name: 'JsonStore',
            replace: 'updateJsonStore',
            remove: 'deleteJsonStore',
            add: 'addJsonStore',
            load: {
                procedure: 'getJsonStores',
                params: [],
                key: 'faml'
            },
            accept: function (data) {
                return (data.status === 200);
            }
        }
    }
}, {
     password : 'PleaseChangeThisPassword'
})

.then(function () {
    WL.Logger.debug(['Take a look at the JSONStore documentation and getting started module for more details and code samples.',
        'At this point there is no data inside your collection ("faml"), but JSONStore is ready to be used.', 
        'You can use WL.JSONStore.get("faml").load() to load data from the adapter.',
        'These are some common JSONStore methods: load, add, replace, remove, count, push, find, findById, findAll.',
        'Most operations are asynchronous, wait until the last operation finished before calling the next one.',
        'JSONStore is currently supported for production only in Android and iOS environments.',
        'Search Fields are not dynamic, call WL.JSONStore.destroy() and then initialize the collection with the new fields.'].join('\n'));
})

.fail(function (errObj) {
    WL.Logger.ctx({pretty: true}).debug(errObj);
});

}());

当我点击登录按钮时,我会像这样调用 getJsonStores -

getJsonStores = function(){

    custData = responseData();
            var invocationData = {
                    adapter : "JsonStore",
                    procedure : "getJsonStores",
                    parameters : [custData],
                    compressResponse : true
            };
            //WL.Logger.debug('invoke msg  '+invocationData, '');
            WL.Client.invokeProcedure(invocationData, {
                onSuccess : sucess,
                onFailure : AdapterFail,                
                timeout: timeout
            });

    };

我关注了these steps 这是正确的方法吗?以及如何检查 jsonstore 在本地工作与否?以及如何将我的 jsondata 存储在 JSONStore 中?我应该在哪里初始化项目中的 wlCommonInit 函数?

请帮帮我。

【问题讨论】:

    标签: json ibm-mobilefirst worklight-adapters jsonstore


    【解决方案1】:

    打开main.js,找到wlCommonInit函数,添加JSONStore初始化代码。

    WL.JSONStore.init(...)
    

    您已经有一个适配器,可以返回您要添加到 JSONStore 的数据,在 init 完成后随时调用它。

    WL.Client.invokeProcedure(...)
    

    onSuccess 回调中,当您从适配器成功获取数据时执行的函数,开始使用 JSONStore API。编写代码的一种高级方法是,如果集合为空(count API 返回 0),则将所有文档添加到集合中。

    WL.JSONStore.get(collectionName).count()
    .then(function (countResult) {
      if(countResult === 0) {
        //collection is empty, add data
        WL.JSONStore.get(collectionName).add([{name: 'carlos'}, {name: 'mike'}])
        .then(function () {
          //data stored succesfully
        });
      }
    }); 
    

    您可能想要添加从适配器返回的数据,而不是添加 [{name: 'carlos'}, {name: 'mike'}]

    稍后在您的应用程序中,您可以使用 find API 取回数据:

    WL.JSONStore.get(collectionName).findAll()
    .then(function (findResults) {
      //...
    });
    

    还有一个接受查询的 find API(例如{name: 'carlos'}),请查看入门模块here 和文档here

    值得一提的是,JSONStore API 是异步的,你必须等待回调才能执行下一个操作。

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 2013-08-13
      • 2015-06-07
      • 2015-02-16
      • 2013-12-07
      • 2014-10-22
      • 2014-01-08
      • 2013-12-27
      • 2013-07-18
      相关资源
      最近更新 更多