troyanskiy / ngx-resource

Resource (REST) Client for Angular 2
http://troyanskiy.github.io/ngx-resource/
200 stars 46 forks source link

Abort request in 7.0? #152

Closed adrianmarinica closed 5 years ago

adrianmarinica commented 5 years ago

What would be the recommended way to abort a request with the 7.0 functionality? I found an abort method in the IResourceHandlerResponse interface, but I can't seem to find the necessary objects to use it. The only object of this type is the result of the handle method in the ResourceHandler object, but this handle method requires an IResourceRequest object that I have no idea where to get.

Is this still a supported functionality? Thanks!

troyanskiy commented 5 years ago

Hello @adrianmarinica It is still supported, but I would recommend to use observable method and unsubscribe if you need to cancel the request.

ex:

@Injectable()
@ResourceParams({
  .... your options
})
export class MyResource extends Resource {

  @ResourceAction({
     .... your options
  })
  someMethod$: IResourceMethodObservable<any, any>;
}

// component
export class MyComponent implements OnInit, OnDestroy {

  data: any = null;

  private subscription: Subscription | null = null;

  constructor(private myResource: MyResource) {}

  ngOnInit() {
    this.subscription = this.myResource.someMethod$.subscribe(data => {
      this.data = data;
      this.subscription = null;
    })
  }

  ngOnDestroy() {
    if (this.subscription) {
      this.subscription.unsubscribe();
      this.subscription = null;
    }
  }

}
adrianmarinica commented 5 years ago

Thanks for the help and for the code sample!