JavaScript Self Updating Objects

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.

If I understood your question correctly, you can totally do that. All that is required is for you to add the fetch/xmlhttprequest call in the ā€œelseā€ block of your code :grinning:

AThanks for sharing! @kirupa

Hello! And thank you very much for your reply.

Fetching the info is not the real problem for me; I am using Axios to handle that part. What I do not know is how the object would ā€˜figure outā€™ it doesnā€™t have the info yet. I am puzzled because I would be accessing a property, not a method. Am I making sense?

For example, I would use the user object as follows:

var userObj = {
  this.name: ā€˜Danielā€™,
  this.mainAddr: ā€˜1488, Market Streetā€™,
  this.realEstate: [],

  getRealEstate: function(){
    If ( this.realEstate ){
      return this.realEstate;
    } else {
      // fetch from API;
      // add to self;
      // return this.realEstate;
    }
  }
};

let name = userObj.name;
let address = userObj.mainAddr;
let assets = userObj.realEstate;

How do avoid getting an empty or undefined result for ā€˜assetsā€™ if the userObj has not yet been populated with the the fetch results?

And if I use ā€™realEstateā€™ as a method [i.e. ā€˜let assets = userObj.getRealEstate( )ā€™], wonā€™t it fetch the data from the API every time?

Something tells me my solution would somehow relate to closures but I donā€™t know how to implement it.

Cheers

You can initialize the objectā€™s properties with some default value. This value would be overwritten with the real value once it is available. You can check for the presence of the default value (maybe the empty [ ] array you already have?)and then fetch from the server if it is present, for that would indicate no prior fetching from the server had already happened.

1 Like