Another improvement to the Resource API would be an option to only accept manual triggers in some scenarios
Currently, the request is made immediately, which means we have to go through some workarounds to ensure, for instance, that the request is only made when a button is clicked
ALT @Component({
template: `
<button (click)="clicks$.next($event)">Load Data</button>
@if (data.hasValue()) {
<app-display-data [data]="data.value()" />
}
`
})
export class SkolComponent {
readonly #dataService = inject(DataService);
click$ = new Subject<MouseEvent>();
latestClick = toSignal(this.click$, {initialValue: null});
data = rxResource({
request: () => ({clicked: this.latestClick()}),
loader: ({request}) => {
if (!request.clicked) {
// I have to do this to prevent an HTTP
// call until the user clicks on the button
return of(null);
}
return this.#dataService.getData();
},
});
}