I am facing this situation:
I have a frontend which calls a REST endpoint to obtain some data and displays the data in a table.
For the frontend side, I am using Angular2 with Material design in Typescript.
I have a Service that was giving fake data while developing the frontend and now I am converting it to use angular Http service, to get the data from the actual backend.
this is an example kinda like the service I am using:
@Injectable()
export class ExampleService{
getData(): Observable<DataType>{ ... }
doStuffOnData(): Observable<boolean>{ ... }
}
Now, the results of getData()
is injected directly into a DataSource
which is fed to a MatTable
, this way, when I was faking the backend, I could just return a BehaviourSubject
and use next
on each action that modifies the data.
Now that I am using the backend, first thing I did has been to send the Observable result of http.get
directly to the table, and this works, but I don't know how to handle modifications.
Two possibilities come to my mind:
- Send a new GET request after each POST to retrieve the server-side updated data
- Cache the data in the service and modify manually after each successful request is made.
Does something else exist? Let's say I want to send a new GET, can I somehow get a "recurringGET" that I can send multiple times and it gives me the results always on the same observable? Something like:
@Injectable()
export class ExampleService{
getData(): Observable<DataType>{ return http.recurringGET(); }
doStuffOnData(): Observable<boolean>{ http.reissueGET(); }
}
I also fully control the backend, so if a smart solution needs to change the return of the call, it can be done. The backend is in raw PHP (yeah, painful, but the only possible solution given my hosting)