Search code examples
jqueryjsondata-objects

How to build data object in browser?


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?


Solution

  • 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.