Good day folks, I have this crazy idea to use service as cache store. so I have defined few variables in Service. The data is updated all right in service but changes not reflected on view. View gets already populated and service layer runs afterward. I am not sure, even if it is a good idea to store data in service? can some one shed light on it please. here is my service code:
categories : SelectItem[]=[];
constructor(private utilityService:UtilityService) {
}
//populate all drop downs array with initial data from db
populateAll() {
//get categories
this.getCategories();
//there are lot more calls here
}
private getCategories() {
if(this.categories.length==0){
this.utilityService.getAllCategories().subscribe(cat=>{
this.categories =
this.utilityService.getSelectItemPublished(cat,"Category");
})
}
}
This is how I call it in component
constructor(
private cache:CacheStoreService,
) { }
ngAfterViewInit()
{
this.categories=this.cache.categories;
this.depts=this.cache.depts;
this.focuses=this.cache.depts;
this.statuses=this.cache.statuses;
this.phases=this.cache.statuses;
this.visibilities=this.cache.visibilities;
}
ngOnInit() {
this.cache.populateAll();
Appreciate your help. One more question: How can I update the service varaibles, when new data is added in db-update the cache. **I know, i can return observable and subscribe to it in component. I am looking another way. Not sure, if it is do able? **
Thank you