0

I have graph with data in welcome page like widget(/welcome). when the user clicks the graph the page change to /home/default and the same graph should be displayed along with some extra data which is populated by Ajax call. What I want is to persist the graph data from /welcome into /home/default page. I don't want the data to go controller and back to the /home/default page. Kindly suggest.

gauti
  • 527
  • 1
  • 6
  • 16
  • local storage or cookies – sumeet kumar Sep 21 '15 at 17:58
  • You mention a _controller_... does this mean you are using a framework? If so, what framework? This information will make the answers more useful to you. Currently, all someone could offer is generic implementation details. – sdgluck Sep 21 '15 at 18:07
  • thanks. I'm using spring framework. – gauti Sep 21 '15 at 18:33
  • Store the graph data in localStorage. Then add some javascript to the `/home/default` page that will check to see if the graph data exists in localStorage instead of making an AJAX request for the data. – Cory Danielson Sep 21 '15 at 18:51

2 Answers2

0

In a nutshell, you need to set some state for the user and then when the /home/default page is rendered, you need to check that state and make corresponding changes to the presentation of the page.

This can be done either server-side (in the rendering of the page) or client-side (via Javascript adding things to the page).

If you do this client-side, then the state can be stored in a cookie, in LocalStorage or in a query parameter in the URL when you redirect. Then, you place Javascript code into /home/default that checks that state and adds content to the page dynamically based on the state.

If you do this server-side, then the state can be stored in a cookie or in some server-side data store and then when the /hoome/default page is rendered, your server side page rendering process can check the state for this particular user and modify the rendering of the page to include the desired content.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • If the graph data contained thousands of points of data, storing it in a cookie might not be the best way to go, because it would increase the payload of every page, right? – Cory Danielson Sep 21 '15 at 18:55
  • @CoryDanielson - yes, you do not want to store large things in a cookie. They should either be stored server-side in a database/cache or client-side in LocalStorage. Be aware that even local storage has limits. – jfriend00 Sep 21 '15 at 18:58
0

You have a plethora of options. The best solution depends on how your application is currently implemented -- whether in a framework or not, with sessions or not, etc. The principle whatever method you choose is almost identical: store a value and then retrieve it later.

Single Page Application (SPA)

If you aren't already using a framework, I would urge you to consider migrating to one as tasks like these are made infinitely more elegant in their implementation.

Service / Data Store

If you are building an SPA then you may not have to consider any of the options below... so long as it doesn't matter if the data is lost if the user performs a 'real' navigation that cannot be intercepted by the framework (for example, refreshing the page).

In Angular you can maintain a temporary data store in the form of a service. Simply store the data and then pick it up later from another controller. Similar functionality can be achieved in all other popular SPA frameworks:

Local Storage

Local Storage is available in IE8 and above and has a really simple API.

IndexedDB

If you're into the cutting edge and aren't tied down by browser support, consider using IndexedDB. I don't recommend using this unless you are wanting to persist large amounts of data remotely on the client's machine. (It really does have bad support at the moment.)

Cookies

If your application is inflexible then cookies will be the easiest and least time-consuming. However Local Storage may be a contender.

sdgluck
  • 24,894
  • 8
  • 75
  • 90
  • this answer is overkill. you're asking him to rethink the entire architecture just to solve a problem that could be fixed by storing something in localStorage? – Cory Danielson Sep 21 '15 at 18:48
  • @CoryDanielson I'm not asking anything of them - OP asked for suggestions. It is unclear what the best solution is considering very few details are given so I figured it's better to demonstrate all the tools at their disposal! (I did also mention local storage, however it _may_ not be the best solution in this instance.) – sdgluck Sep 21 '15 at 18:58