ollieyeahlong
ollieyeahlong

Reputation: 68

How can I reuse a javascript object which is returned to the client when a page is loaded?

When I first load a page, an AJAX request is used to return a list of javascript objects and display just one of each of their values.

On the page there are buttons to expand each item and view the rest of their values. My initial approach was to use AJAX again to get each individual object when they are expanded and display all the data this time, however it occurred to me that if I am loading all these values when the page is loaded, surely they should be accessible in some way for later use.

I am sure I am missing an obvious solution; is it as simple as declaring the list as a global variable?

Edit: Another feature is that I can add new items to this list from the page, in which case the new object would have to be added to the global variable as well as to the database. Would it then just be easier to request each object from the server again when they are clicked?

Upvotes: 0

Views: 210

Answers (1)

mohamed alattal
mohamed alattal

Reputation: 76

you should indeed save it to a global variable and use that variable in future operations to that list. Regarding the other question in the edit, it will be a lot faster to append it to the list locally and still use the variable but that can cause problems in case the addition to database failed. So for the case of consistency of data you should add the element to the list after it is successfully added to the database.

Upvotes: 1

Related Questions