Page 1 of 1

download DB via REST service and store each item in local storage.

Posted: Wed Aug 13, 2014 12:24 am
by FrankV

I want to press a button and read a cloud database and store each item into local storage. I have successfully run the basic tutorials on creating the db (3 or more rows and 2 columns like testName and testValstring), reading that via rest service, and mapped those to predetermined select objects but I can't seem to figure out how to store each name and val into local storage. I assume some sort of JS for loop that creates the local db names with same value as the JSON returns but I could use some code help or a magical service mapping.


download DB via REST service and store each item in local storage.

Posted: Wed Aug 13, 2014 5:09 am
by Yurii Orishchuk

Hi FrankV,

Here is solution for you:

1 Open your page where you have this service.

2 Navigate to "data" page mode.

3 Open "Events" bottom panel.

4 Add "Success" event handler on your service. Select action "Run javascript".

5 Populate it with following code:

pre

localStorage.setItem("itemsArray", "[]");

var AddItemToLSVArray = function(lsvName, value){

Code: Select all

 var lsv = localStorage.getItem(lsvName); 

 if(!lsv) 
     lsv = "[]"; 

 var currentObject = JSON.parse(lsv);  

 currentObject.push(value); 

 localStorage.setItem(lsvName, JSON.stringify(currentObject)); 

};

for(var i = 0; i < data&#46;length; i++){
AddItemToLSVArray("itemsArray", data);
};

/pre

See details: http://prntscr.com/4cazsr/direct

Now you can see all items from this service in your LSV.

For example you can run following code in browser console to see what stored in this LSV:

pre

console&#46;log(localStorage&#46;getItem("lsvName"));

/pre

Regards.


download DB via REST service and store each item in local storage.

Posted: Wed Aug 13, 2014 7:12 pm
by FrankV

Thanks, that helps quite a bit. I was originally thinking there would be an lsv for each id (in my specific case the Model value) but guess I can live with a single lsv array if I can code the right index to read the data.
in the browser debugger I can see the lsv but the console.log example just returns null. Here is the item list: [{"id":"53e42a9ce4b01c1e85887742","Model":"V10000G3","tps":"1328","createdAt":"2014-08-08 01:40:44.552","updatedAt":"2014-08-08 01:40:44.552"},{"id":"53e42a9ce4b01c1e85887743","Model":"V10000G2R2","tps":"1129","createdAt":"2014-08-08 01:40:44.553","updatedAt":"2014-08-08 01:40:44.553"},{"id":"53e42a9ce4b01c1e85887744","Model":"V5000G2R2","tps":"598","createdAt":"2014-08-08 01:40:44.554","_updatedAt":"2014-08-08 01:40:44.554"}]

What I would like to do is read Model value and tps value and put that into a label component. can you give me an example how to do that please? Obviously I am not that good at JS yet.
Thanks!


download DB via REST service and store each item in local storage.

Posted: Thu Aug 14, 2014 12:48 am
by Yurii Orishchuk

Hi FrankV,

Here is correct code to get LSV:

pre

console&#46;log(localStorage&#46;getItem("itemsArray"));

/pre

And here is code to iterate through this array. In this loop you can do what you want with each value.

pre

var arr = JSON&#46;parse(localStorage&#46;getItem("itemsArray"));

for(var i = 0; i < arr&#46;length; i++){
var item = arr;

Code: Select all

console&#46;log("item[" + i + "], model = " + item&#46;Model + ", tps = " + item&#46;tps); 

};

/pre

Regards.


download DB via REST service and store each item in local storage.

Posted: Thu Aug 14, 2014 1:16 am
by FrankV

Yurri,
Awesome! Thanks!


download DB via REST service and store each item in local storage.

Posted: Thu Aug 14, 2014 1:47 am
by FrankV

One last thing.
I am not sure how to use apperio() to set the text on the label.
I added 2 lines to your JS but the label text did not change.
var arr = JSON.parse(localStorage.getItem("DBitemsArray"));
for(var i = 0; i < arr.length; i++){
var item = arr;
console.log("item[" + i + "], model = " + item.Model + ", tps = " + item.tps);
}
var blort = arr[1];
Apperyio("mobilelabel_88").text = " + blort.Model +";
; Image


download DB via REST service and store each item in local storage.

Posted: Thu Aug 14, 2014 2:16 am
by Yurii Orishchuk

FrankV,

You can use following code for assign some text to appery.io label component:

pre

var text = "Some label text";

&#47;&#47;Note: you need to replace "mobilelabel_88" with your label component name&#46;
Apperyio("mobilelabel_88")&#46;text(text);

/pre

Regards.