Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Help me to save data in local storage.

Hi

 

i need to save some data in local storage can you please help me to make getter or setter method.

 

<body onload="GetStationCodes()"> i call this fuction on body load

 

function GetStationCodes()
{
***************here i want some condition to check is there any data present in local storage or not**********
 var pl = new SOAPClientParameters();

SOAPClient.invoke(WSDLURL, "stationSearch", pl, true, GetStationCodes_callBack);
}

 

function GetStationCodes_callBack(r)
{

for (var i = 0; i < r.length-1; i++)
{
stationList[i] = r[i].stationCode + " - " + r[i].stationName;

 

*************************I want to save this  list ************************
window.localStorage.setItem(i,stationList[i]);
}

}

 

function getter(){

 

I need this getter function.

please help

thanks

Please use plain text.
Developer
Developer
LBP
Posts: 607
Registered: ‎04-29-2011
My Device: PlayBook 16GB, BB10 LE Z10 (red)
My Carrier: none

Re: Help me to save data in local storage.

What you want to do is really heavy, storing each item of your "r" to localStorage. Why not storing complete "r" in localStorage?

 

And, remeber my last post about storing items to localStorage. You use this:

 

window.localStorage.setItem(/* Integer i */, stationList[i]);

But the key needs to be in string representation like this:

 

window.localStorage.setItem("item"+i, stationList[i]);

But doing this for every item in "r" array you will never be able to reload all the items. Why not storing the complete array to one key in localStorage like this:

 

window.localStorage.setItem("stations", JSON.stringify(r));

In that way you are able to get the array of all station items directly using something like this:

 

var r = JSON.parse(window.localStorage.getItem("stations"))||[];
// now do your for-loop
for (var i = 0; i < r.length-1; i++)
{
...
}

Why using JSON.stringify and JSON.parse? There is a good hint on the HTML5 storage overview page for LocalStorage pointing it out:

 

Unfortunately, present implementations only support string-to-string mappings, so you need to serialise and de-serialise other data structures. You can do so using JSON.stringify() and JSON.parse().

You should read more about HTML5 LocalStorage when working with it and if you understand it and run into trouble then ask your questions. A good starting point is this: http://www.html5rocks.com/en/tutorials/offline/storage/

 


"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

Hi

 

I think this is best answer .can you please tell me what is the condition to check is there any value in r or not...

 

var r = JSON.parse(window.localStorage.getItem("stations"))||[];

if(r!=null){
for (var i = 0; i < r.length-1; i++)
{
...
}

}
else
{
SOAPClient.invoke(WSDLURL, "stationSearch", pl, true, GetStationCodes_callBack);
}

is this write..?

 

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

Hi

 

I want  to ask one more question if i used local storge ,and i  saved data but when i delete my app from device  storage data is deleted or not.? and when put new build to device it  it first download from server or use previous local storage

Please use plain text.
Developer
Developer
LBP
Posts: 607
Registered: ‎04-29-2011
My Device: PlayBook 16GB, BB10 LE Z10 (red)
My Carrier: none

Re: Help me to save data in local storage.

[ Edited ]

For your previous question, about to check the "r" have you tried to check what this will throw you in the Web Inspector console:

 

var r = JSON.parse(window.localStorage.getItem("stations"))||[];
console.log(r);

If your server sends an array and it is already stored it will produce the array of data, else it will produce an empty array. So how to check if an array has data?

 

if(r.length > 0) {
 // you already downloaded the data from service
} else {
 // download and store data since nothing available in client
}

 

LocalStorage, as far as I know, should be update save, so if you have data stored in your app and update, the data should still be available.

 

If you delete the app from device it should also delete the data since it is part of the app and not part of something else.

 

Next question would be, how to make an update of the data, isn't it? Store timestamp in data that holds the date until the data is valid, if local timestamp on the app/device exceeds this data-timestamp reload the data.

 

BTW: There is a like button if you like those answers, isn't it. I think many devs around here tried to answer your questions and your app should be completely done with this help, so give something back to them. On the other hand, why not hiring somebody for all this to avoid asking and also get it ready some day since it sounds you are more tinkering than developing the app, isn't it?

 


"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

yes sir you are right..

 

 

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

Hi

sir i got lot of learning from this blackberry forums. one simple question but it hard to answer actually i am begineer ,

last 6 month i am blackberry developer done coding in eclipse , but last one month i am working in web work.

actually every body say use web inspector or something.

 

trutly speaking where you do coding?  actually i done my coding in note pad and save all .html file sample folder ,run from command promt, not able to debug , for bebug i used alert.

 

sir  do you have any software where i tool just like eclipse so that i can debug , or get some error(if in the code)

 

please tell so that i will download that tool 

 

Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

For only phonegap....

Please use plain text.
Developer
Developer
LBP
Posts: 607
Registered: ‎04-29-2011
My Device: PlayBook 16GB, BB10 LE Z10 (red)
My Carrier: none

Re: Help me to save data in local storage.

Web Inspector is part of the WebKit engine every WebWorks app is based on since they are WebApps. For the Web Inspector all you need to do is enabling the developer mode (you already should have done to deploy to the device) and packaging your app with the -d command line operator. Have a closer look to the BlackBerry devblog post about that: http://devblog.blackberry.com/2011/06/debugging-blackberry-web-apps/


"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
Please use plain text.
Developer
ravi1989
Posts: 777
Registered: ‎07-21-2012
My Device: 9810
My Carrier: 0

Re: Help me to save data in local storage.

I just want where you write you code instend use note pad is there ant tool just like eclipse in java blackberry..?

Please use plain text.