Page 1 of 1

Storing multiple JSON arrays from REST response in localStorage

Posted: Mon Mar 31, 2014 4:46 am
by tim foran

I have an app that is based around a search function. The results returned are relatively complex, and each result includes a JSON array with data unique to the search just performed.

I need to find a way to store the JSON array from each result so that it can be used on a detail page that is access by clicking the specific result. What I am thinking is to use JSON.stringify(), but I am not sure how to set that in the data mapping so that each result's array is stored properly in local storage.

Can somebody guide me on how to do that? Thanks!


Storing multiple JSON arrays from REST response in localStorage

Posted: Mon Mar 31, 2014 6:00 am
by Kateryna Grynko

Hi Tim,

Check this conversation please:
https://getsatisfaction.com/apperyio/...


Storing multiple JSON arrays from REST response in localStorage

Posted: Mon Mar 31, 2014 6:53 am
by tim foran

Hi Katya,

Thanks for the quick reply. I will look into this.

How do I use the code mentioned in that conversation to pull directly from the REST response and into local storage? Where/when do I run that?

I have an array for each result, and depending on the result selected to be viewed in Details, I need to be sure I choose the correct array.


Storing multiple JSON arrays from REST response in localStorage

Posted: Mon Mar 31, 2014 7:36 am
by Kateryna Grynko

Tim,

You can add a service Success event handler. Access the service response using variable "data". You can save it all:prelocalStorage.setItem("myResponse",JSON.stringify(data));/pre


Storing multiple JSON arrays from REST response in localStorage

Posted: Tue Apr 01, 2014 7:30 am
by tim foran

Hi Katya,

That makes sense. So I save the entire response to localStorage, and then access it on the next page with getItem("myResponse").

I can then convert it back to JSON, then just search the entire response for the object that has the same ID as the result I'm looking at. Once I have that, I can return the array needed for that result.

Am I understanding this correctly?

Thanks!


Storing multiple JSON arrays from REST response in localStorage

Posted: Tue Apr 01, 2014 8:52 am
by Kateryna Grynko

Hi Tim,

Yes, you're right.


Storing multiple JSON arrays from REST response in localStorage

Posted: Wed Apr 02, 2014 7:34 am
by tim foran

Hi Katya,

I wrote the following code, but am having trouble getting it all to execute. I am basically extracting the array I get from localStorage and parsing it back to JSON, and then using an array from that to create a chart. It is telling me that my function 'length' is undefined:

//retrieve initial results response from localstorage
var search_results = localStorage.getItem('results_response');

//convert retrieved string back to JSON array
var search_results_object = JSON.parse(search_results); //search_results_objects is now a JSON array

var results = search_results_object.results; //pull out results array

//get venue_id of detail page from local storage
var ven_id = localStorage.getItem('venue_id');

//search JSON response for venue ID
var chartDataResults = [];
var searchField = "id";
var searchVal = ven_id;
for (var i=0 ; i < results.length ; i++)
{
if (results[searchField] == searchVal) {
chartDataResults.push(results);
}
}

var chartDataInput = chartDataResults.essence_tags;
//this all works

var chartData = chartDataInput;

var chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "tag";
chart.rotate = true;

var graph = new AmCharts.AmGraph();
graph.valueField = "relevance";
graph.type = "column";
chart.addGraph(graph);

var categoryAxis = chart.categoryAxis;
categoryAxis.autoGridCount = false;
categoryAxis.gridCount = chartData.length;
categoryAxis.gridPosition = "start";
categoryAxis.labelRotation = 90;
graph.fillAlphas = 0.8;
chart.write('chartdiv');

Any thoughts? Thanks!


Storing multiple JSON arrays from REST response in localStorage

Posted: Wed Apr 02, 2014 8:26 pm
by Maryna Brodina

Hello!

Please try to debug app. Most likely you expect array, but there is not array. Set breakpoints in code and go through step by step.