2

I want to know that can we build a data object using jquery/json, which only exist in the browser like we have session object at server side?

The reason for having a data object is that I am providing different layouts for users so they can manipulate data with out fetching it from the server every time they change the layout i.e. like a user has his purchase history for last 6 months and he want to see the bar graph of it history depending upon different metrics like how much they purchased using cash/credit card/ debit card or in which month they have spent more than other months ...

the one good example is the reputation system of stackoverflow where at the bottom it shows the full reputation stats but when we select the particular time, it expands the above graph to that period, and when we click any of the bar on the top graph it shows the questions which has been voted up/down ... something like a database in the browser.

I hope I have clearly explained what I am looking for?

Safran Ali
  • 4,477
  • 9
  • 40
  • 57
  • Are you talking about cookies? – Jivings Feb 07 '12 at 15:23
  • I don't see how you would do this without having to use AJAX to request data from the server. Even using cookies would not be a good idea because it sounds like you are trying to persist quite a bit of data here. If you'd be interested in an AJAX and JSON based solution I can provide an answer. – bsimic Feb 07 '12 at 15:29
  • not really, because in cookies you have the limitation for data storage ... I am looking for a JSON data object which is created when user logs in and destroy when user logs out ... – Safran Ali Feb 07 '12 at 15:30
  • @bsimic I know about Ajax and JSON, and I am using the same approach but I am looking for a best practice which involves less DB usage when you are manipulating the data at client side ... and JSON have very rich data structure model to hold such kind of data ... please do present your idea you might using better approach then what I am doing now ... – Safran Ali Feb 07 '12 at 15:36

1 Answers1

2

Here is what I would recommend. I am going to go by your suggestion for a user to be able to

a user has his purchase history for last 6 months and he want to see the bar graph of it history depending upon different metrics like how much they purchased using cash/credit card/ debit card or in which month they have spent more than other months

To minimize query time, I would create a materialized view that holds data that would be necessary for this functionality. I assume that this view would not always have to be up to date and you could re-create it at an off-hour every day or something.

Then I would a class that would solely serve to fulfill AJAX requests to retrieve this data. You should create objects that have accessors (getters) that represent the values you are trying to display. Then convert those objects to JSON and have that be the return value of your AJAX request.

Once you have it back on your HTML page, you can do whatever you want with it since it is in JSON format. You could also put the result of your query converted to JSON on the session object so you can have access to it on every page.

I hope that helps. Feel free to message me if you need any clarification.

bsimic
  • 926
  • 7
  • 15
  • thanks I had the same idea in my mind, one more thing do you have any tutorial or example which I can look? As it'll be a good thing as I am not to expert on programming in JQuery object handling ... – Safran Ali Feb 09 '12 at 08:47
  • 1
    Really the jQuery website is super helpful and has great examples. Just looking at http://api.jquery.com/jQuery.getJSON/ has some really great info. Once you get the JSON in a jQuery object, the object handling is really similar to Java. – bsimic Feb 09 '12 at 23:42