johannesjo / angular2-promise-buttons

Chilled loading buttons for angular2
https://johannesjo.github.io/angular2-promise-buttons/#demo
MIT License
86 stars 28 forks source link

Observable #5

Closed hvqthong closed 7 years ago

hvqthong commented 7 years ago

Can you guys support for Observable? Thanks 😀

johannesjo commented 7 years ago

Good suggestion. I'll work on that, once I'm back from vacation.

johannesjo commented 7 years ago

@KeitaElric I'm familiar with the basic concept about observables but not too much with the specifics. I wonder what types of observables would you like to be supported. Could you maybe provide a specific use case scenario? That would be super helpful!

johannesjo commented 7 years ago

It already works if you do it like this:

 initObservable() {
    // const obs = FAKE_FACT.initObservable();
    const obs = new Observable(observer => {
      setTimeout(() => {
        observer.next(1);
      }, 1000);

      setTimeout(() => {
        observer.next(2);
      }, 2000);

      setTimeout(() => {
        observer.next(3);
      }, 3000);

      setTimeout(() => {
        observer.complete();
      }, 4000);
    });

    this.obsVal = 'INITIALIZED';

    this.observableItem = obs
      .forEach((val) => {
        this.obsVal = val;
      })
      .then(() => {
        console.log('OBS COMPLETED');
        this.obsVal = 'COMPLETE';
      });
  }
<button class="btn btn-raised"
        (click)="initObservable()"
        [promiseBtn]="observableItem">Observable {{ obsVal }}
</button>

As long as there is a then, you should be fine.

johannesjo commented 7 years ago

Thanks to @maxkorz this should work with any kind of observable now.