Search code examples
node.jsangularrestful-architecture

Is it an acceptable practice to store object instance data on the front end in an Angular2 application?


When developing an Angular2 app with a node.js backend is it best practice to store only the id for an object and make an api call every time a specific instance of an object is needed?

for example if I have a user like this:

{public email: string,
 public password: string,
 public roles?: Role[],
 public firstName?: string,
 public lastName?: string,
 public _id?: string}

and on my frontend I have made an API call for a list of users that can be selected and edited. Would it be best practice to make an API call to get the specific user to edit? Or should I make the API call to get the list of users and store it an an array on the frontend like this:

[{user}, {user}]

then submit a patch request once done editing?

My concerns are that the data state on the server may change between when the API call for the list is made and when the user is edited. However it may be faster to store the items and retrieve it from memory than to make an API call.


Solution

  • My concerns are that the data state on the server may change between when the API call for the list is made and when the user is edited. However it may be faster to store the items and retrieve it from memory than to make an API call.

    In this case making a call that will return a list of [user1, user2, ..] is a bad idea because, indeed the data will become old quickly.

    The good advice is to just load as much data as needed to display the page correctly. If you need to show a list of data, try to take few details (possibly the less variable ones) and once a record is selected from this list, then you make an api call to fully load the data related to the particular record and perhaps populate a detailed view.