user3747168
user3747168

Reputation: 43

IBM Worklight JSONStore - Add and get data

I am using worlight JSONstore. I am new to it. I tried searching that read all docs but didn't get much idea.

I have one login page from that I get some json data I want to store that data using jsonstore. and get that afterwards.

I made jsonstore adapter.

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);
}

after that I Create a local JSON store.

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);
});

}());

When I clicked on login button I call getJsonStores like this -

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
            });

    };

I followed these steps Is this right way? and how can I check jsonstore working locally or not? and how can I store my jsondata in JSONStore? Where should I initialize the wlCommonInit function in project?

plz Help me out.

Upvotes: 2

Views: 3221

Answers (1)

cnandreu
cnandreu

Reputation: 5111

Open main.js and find the wlCommonInit function, add the JSONStore init code.

WL.JSONStore.init(...)

You already have an adapter that returns the data you want to add to JSONStore, call it any time after init has finished.

WL.Client.invokeProcedure(...)

Inside the onSuccess callback, a function that gets executed when you successfully get data from the adapter, start using the JSONStore API. One high level way to write the code would be, if the collection is empty (the count API returns 0), then add all documents to the collection.

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
    });
  }
}); 

Instead of adding [{name: 'carlos'}, {name: 'mike'}] you probably want to add the data returned from the adapter.

Later in your application, you can use the find API to get data back:

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

There is also a find API that takes queries (e.g. {name: 'carlos'}), look at the getting started module here and the documentation here.

It's worth mentioning that the JSONStore API is asynchronous, you must wait for the callbacks in order to perform the next operation.

Upvotes: 3

Related Questions