Hi! I am trying to develop the server side of a user dashboard web site. I am using Node and Express, and the tool should be basically a middleware between the browser and our API.
I would like to create an object which describes the user and, upon validation and successful login, fetches the latest info from our API making it readily available for display when requested by the browser.
Because of the amount of information involved, I would like to access the API just when the info is first needed and then have the object provide said info in all future requests (on that session, that is).
For example, basic ID info is fetched from the API when the user sends his login credentials. After that, personal details like contact information etc need not be requested from the API anymore. They would be available from the user object. Other info would be fetched only the first time they are needed, being available locally after that.
So here comes the question and title of the topic:
Is it possible to have a JavaScript object āself updateā?
Say, for example, that I would like a list of real estate owned by the user ( e.g userObj.realEstate, which returns an array of objects ). Is it possible to create an object that will return that array if it is already part of the object but will fetch the info and add to the object if that is the first time such info is requested?
Something like:
If ( userObj.realEstate ){
return userObj.realEstate;
} else {
// fetch from API;
// add to userObj;
// return userObj.realEstate;
};
But from within the object itself.
Suggestion are appreciated.