json - IBM Worklight JSONStore - 添加和获取数据

标签 json ibm-mobilefirst worklight-adapters jsonstore

我正在使用 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函数?

请帮帮我。

最佳答案

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

WL.JSONStore.init(...)

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

WL.Client.invokeProcedure(...)

onSuccess 回调(当您成功从适配器获取数据时执行的函数)内,开始使用 JSONStore API。编写代码的一种高级方法是,如果集合为空(计数 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'}]

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

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

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

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

关于json - IBM Worklight JSONStore - 添加和获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256417/

相关文章:

tls1.2 - 无法从 Worklight HTTP 适配器访问在 TLS1.2 上配置的 WebService

android - 用于多个 Worklight 应用程序的推送适配器

javascript - 将 javascript 对象或数组转换为 json 以获取 ajax 数据

python - 谷歌应用引擎 jsonpickle

push-notification - 工作灯 : Push notification without User ID

ibm-mobilefirst - 我们如何在 MobileFirst 中实现添加到任何(社交网络)

javascript - 禁用输入字段上的文本选择

json - 从 SQL Server 中删除重复的 JSON 节点

python - 将 JSONL 文件加载为 JSON 对象

mysql - 在 Worklight 平台或项目错误中找不到 com.mysql.jdbc.Driver