My current strategy involves refreshing the Details
page after a new record is added, as well as after updating a record.
DataRefreshService
export class DataRefreshService {
private dataTracker = new BehaviorSubject<any>();
getData(): Observable<any> {
return this.dataTracker.asObservable();
}
setData(param: any): void {
this.dataTracker.next(param);
}
}
CreateComponent:
export class CreateComponent implements OnInit {
constructor(private dataRefreshService: DataRefreshService) { }
create(param: any): void {
//other actions related to record creation
this.dataRefreshService.setData(param);
}
}
DetailsComponent:
export class DetailsComponent implements OnInit {
subscription;
constructor(private dataRefreshService: DataRefreshService) { }
ngOnInit() {
this.subscription = this.dataRefreshService.getData().subscribe((param: any) => {
this.updateRecord(param);
);
}
updateRecord(param) {
this.record = param; //updating record with new data from service
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
}
While my approach works effectively, I have some concerns:
1) Given that I call
this.dataRefreshService.setData(param);
in both the create()
and update()
methods, should I consider moving this operation to the service level instead of the component level?
2) Is there anything problematic about invoking the next() method in the CreateComponent
as shown above?