Page 1 of 1

local storage array to list item

Posted: Mon Mar 16, 2015 3:09 pm
by Joe Sharples

I'm trying to create a service that saves the response into a few local storage arrays. and then on different events map the different local storage array to a listitem.
This way users can use different events to change what results are shown on the list without having to call the service multiple times.

I have the model:
Image

The Venue model contains the parameters that match the fields of the service response exactly.

On the service response I have the JS:

prevar AllArray = [];
var BarsArray = [];
var ClubsArray = [];

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

AllArray&#46;push(data);

Code: Select all

 if(data[i]&#46;VenueType == "Bar"){ 
     BarsArray&#46;push(data[i]); 
 } 

 if(data[i]&#46;VenueType == "Club"){ 
     ClubsArray&#46;push(data[i]); 

}
}

localStorage&#46;setItem("AllVenues", JSON&#46;stringify(AllArray));
localStorage&#46;setItem("BarVenues", JSON&#46;stringify(BarsArray));
localStorage&#46;setItem("ClubVenues", JSON&#46;stringify(ClubsArray));
/pre

This iterates through the response and puts the data in separate local storage arrays, based on the 'VenueType' db field.

I then map the 'AllVenues' array to a list:
Image

Now, for some reason this doesnt work.

when I test the app, the results are successfully stored in the local storage, but the mapping doesnt work. the list component just isnt there (I have checked the visibility of the list, and I even added a button on the page to set the list visibility property to true, just to make sure that something wasn't hiding the list)

But it still doesnt work. I have the same method on another page and it works there.

Using the same method, I have a similar service on another page that iterates through the response and saves the results in separate storage arrays, with similar code. and then the storage array is mapped to a list component in the same way...
the only difference in the code is:
prevar othernightsArray = [];
var mainnightsArray = [];
for(var i = 0; i < data&#46;length; i++){
if(data&#46;NightName == "Open")
othernightsArray&#46;push(data);
else
mainnightsArray&#46;push(data);
}
localStorage&#46;setItem("OtherNights", JSON&#46;stringify(othernightsArray));
localStorage&#46;setItem("MainNights", JSON&#46;stringify(mainnightsArray));
/pre

I really don't understand why the first example is not working and yet the second one is.


local storage array to list item

Posted: Tue Mar 17, 2015 7:06 am
by Egor Kotov6832188

Hello Joe,
localStorage is not part of Model&Storage Variables.
If you create variable Model&Storage, then setting it will be next line:
Apperyio.storage.MYVAR_NAME.set("VALUE");

instead of localStorage.setItem("MYVAR_NAME","VALUE");


local storage array to list item

Posted: Tue Mar 17, 2015 10:49 am
by Joe Sharples

Hi Egor,

Thanks.
Your reply made me take a closer at my code.

prelocalStorage&#46;setItem("AllVenues", JSON&#46;stringify(AllArray));/pre

The problem was the desired local storage was 'AllArray', not 'AllVenues'.
A simple silly mistake.

Thank you for your help