Nick
Nick

Reputation: 17

Is it normal practice storing each request in the object in angular?

I have a code for storing/caching each(or almost each) request in the object, but not sure is it good practice or not.

store = [];
fetchAll(params: any, cache = true): Observable<PostsInt[]> {
 let posts = this.http.get<PostsInt[]>(this.getApiUrl('/posts'), {
   params
 });

 if (cache) {
   const key =
     Object.values(params).join('') + APP_CONFIG.data.defaultLocale;
   if (!this.store[key]) {
     posts = posts.pipe(
       publishReplay(1),
       refCount()
     );
     this.store[key] = posts;
   }
   return this.store[key];
 }
 return posts;
}

Upvotes: 1

Views: 70

Answers (1)

liqSTAR
liqSTAR

Reputation: 1315

You dont need to. It depends on the loading balance and the things you want to do with those informations afterwards. There is already an similiar Stackoverflow Question/Answer which answers it. For example: Simple CRUD-Applications dont need to store any informations, thats why they are CRUD-Applications. Another fresh post from April 2019, Store and Redux depends on what framework you are using exactly.

Upvotes: 1

Related Questions