Making a Service Observable

September 4, 2023 · 1 min readLucian Ciobanu
Tags:AngularRxJS

Today I came across a really cool thing I wanted to share! I'm using Kendo UI to create an inline editable table and the documentation presents the coolest way to share data from the edit service to the other components by making the service class extend that of a BehaviorSubject and using super to trigger the next property as such:

edit.service.ts

//...

@Injectable()
export class EditService extends BehaviorSubject<Model[]> {
  private data: Model[] = [];

  constructor(private http: HttpClient) {
    super([])
  }

  public read(): void {
    //fetch makes an http request
    this.fetch().subsribe((data) => {
      super.next(data)
    }
//...  
  }
}

And in the component we want to use the data:

//...

public ngOnInit(): void {
  this.view = this.editService.pipe(
    map((data) => proces(data, this.grid))
  );
}

It's the first time I come across anything like it; there is the link for in depth studying: https://www.telerik.com/kendo-angular-ui/components/grid/editing/in-cell-editing/

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery