Thursday, 15 August 2013

json - $.ajax and IBM Worklight's JSONstore -



json - $.ajax and IBM Worklight's JSONstore -

i trying store json info returned $.ajax() function, having trouble. below phone call create (which know returns json info properly, have logged (url removed personal reasons).

$.ajax({ type: "get", url: **url returns json data** contenttype: "application/json; charset=utf-8", datatype: "json", success: function(msg) { homecoming msg; }, error: function(e){ wl.logger.log(e); } });

and initialize jsonstore using worklight:

//setup json store var collectionname = 'people'; //object defines collections var collections = {}; //object defines 'people' collection collections[collectionname] = {}; wl.jsonstore.init(collections);

so here having issue. want add together json info returning ajax phone call jsonstore. have attempted (getjsondata wrapper around ajax phone call above):

wl.jsonstore.get(collectionname).add(getjsondata());

however, when print collection, nil stored in it, , when print wl.jsonstore.get(collectionname).count, nil returned. how can add together info returned me properly?

both ajax phone call , jsonstore apis asynchronous apis, meaning either have utilize deferred returned them, or pass success callback able wait finish before going next step.

the reason persistent_store_not_open calling init , instead of waiting finish, phone call add together without init beingness done, why says store has not been initialized.

you this:

$.ajax({ type: "get", url: **url returns json data** contenttype: "application/json; charset=utf-8", datatype: "json", success: function(msg) { //setup json store var collectionname = 'people'; //object defines collections var collections = {}; //object defines 'people' collection collections[collectionname] = {}; wl.jsonstore.init(collections) .then(function(res){ wl.jsonstore.get(collectionname).add(getjsondata()); }); homecoming msg; }, error: function(e){ wl.logger.log(e); } });

(note how changed success callback utilize result of ajax call.)

edit: here more info on deferreds , promises in javascript reference: http://code.tutsplus.com/tutorials/wrangle-async-tasks-with-jquery-promises--net-24135

jsonstore returns promise every api phone call can utilize promises handling callbacks instead of having pass success , failure callback everytime.

json worklight jsonstore

No comments:

Post a Comment