简体   繁体   中英

How does it work Observer confirmation?

In the example, what does of() make? How does it work?

 public onRemoving(tag): Observable<any> {
    const confirm = window.confirm('Do you really want to remove this tag?');
    return Observable.of(tag).filter(() => confirm);
}

I tried adapt to my case:

const confirm = false;
this.mapDlg
      .confirm(args)
      .subscribe(res => {
        if (res != null) {
          confirm = true;
        }
      });

return Observable
      .of(tag)
      .filter(() => confirm);

But it does now work, .confirm() returns Observable where I catch value confirm

Also I tried to do this like:

public onRemoving(tag: any): Observable<any> {

    const args = new MultiDlgArgs();
    args.type = 'confirmation';

    return Observable.create(observer => {
      return this.mapDlg
        .confirm(args)
        .subscribe(res => {
          return false;
        });
    });
  }

Another way I have tried:

public onRemoving(tag: any): Observable<any> {

    const args = new MultiDlgArgs();
    args.type = 'confirmation';

    return new Observable<boolean>(observer => {

        this.mapDlg
          .confirm(args).map((res) => {
          if (res == null) {
            observer.next(false);
          } else {
            observer.next(true);
          }
        });

      }
    );
  }

You may think about Observable as about constant stream of data, opposite to Promise that firing only once. .of() - Creates an Observable that emits a sequence of numbers (or any other data) within a specified range. docs

You probably need something like that:

this.mapDlg
      .confirm(args)
      .subscribe(res => {
        if (res != null) {
          Observable.create((observer)=> {
            observer.next(true);
        });
        }
      });

It create an Observable you can subscribe to.

You can see that the function is returning a variable typed as Observable<any> .

This is how I learnt to create Observable, not with the Observable.create method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM