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.
Solution 1 :
Problem :
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?
Comments
Comment posted by chyke007
Have you tried storing the response in a variable?
Comment posted by ollieyeahlong
I have got it to use a global variable but can’t work out if this is secure or not as the data I’m storing is sensitive and should only be global to that page.
Comment posted by chyke007
A global variable would only be global to that page
Comment posted by innerurge1
You could do it either way. Either update the local object when you do the request or get the data again after or depending on how the API works, with the response to the request. Really it depends on how fast the API is, how much data you have to rerender to the page, vs just updating the one bit, especially if you have users with less than amazing computers, and finally how important update that other users might be making at the same time is to your UI. There isn’t a right or wrong way, I think. But bases on how you answer those questions one or the other may be better for your use case.