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
Posts: 281
Registered: ‎05-31-2012
My Device: Playbook
Accepted Solution

how to load a class array?

Hi, I have something like this:

var test = new Array();
test[0] = new Foo(51, 8);
test[1] = new Foo(51, 9);
test[2] = new Foo(50, 7);

// save and load
localStorage.setItem("save", test);
var test2 = localStorage.getItem("save");

 I have my own class called Foo and I save and load it. I wanted to have then the same in test and test2.

 

but when i load the stuff into test2 its not a Foo anymore. How to realize this?

Plase "+Like" my post if it was helpful.
Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10

Re: how to load a class array?

Hi there,

 

localStorage can only save name/value pairs; essentially, strings. You can not save full objects into this storage.

https://developer.blackberry.com/html5/apis/localstorage.html

 

This is where IndexedDB would come in, but currently has not been implemented on the platform.

 

The workaround here would be to save the array into localStorage as strings, potentially even with toString to avoid creating multiple entries:

http://www.w3schools.com/jsref/jsref_tostring_array.asp

 

Then, when you want to create your test2 array, you would need to read that entry, parse it, and populate your array programmatically rather than expecting it to be directly created by the getItem call. For the parsing step, you could use something like split.

http://www.w3schools.com/jsref/jsref_split.asp


Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.