1
votes

I have an observable that will subscribe as usual:

  return oMassChangeOb
    .subscribe(function (aMassDates) {
      const oModel = self.getModel("vmCalSpecialDates");
      oModel.setProperty("/aActualDates", aMassDates);
    }, function (oErr) {
      jQuery.sap.log.fatal(oErr);
    });

My question is, how to run an other observable as soon as the above observable has been subscribed?

I could do it as follow:

  return oMassChangeOb
    .subscribe(function (aMassDates) {
      const oModel = self.getModel("vmCalSpecialDates");
      oModel.setProperty("/aActualDates", aMassDates);
      oSaveCancelFooterStateOb
        .subscribe(function (oBtnState) {
          const oModel = self.getModel("vmButtonsState");
          oModel.setProperty("/bSave", oBtnState.bSave);
          oModel.setProperty("/bCancel", oBtnState.bCancel)
        });
    }, function (oErr) {
      jQuery.sap.log.fatal(oErr);
    });

But I do not know, if it is the right way to go.

There is a notification object that could be solve the scenario, but do not know how to use it.

1

1 Answers

2
votes

From what you are writing, you basically want to run the second observable after the first one finishes, in that case, just chain both into a stream:

return oMassChangeOb
    .do(function (aMassDates) {
      const oModel = self.getModel("vmCalSpecialDates");
      oModel.setProperty("/aActualDates", aMassDates);
    })
    .switchMapTo(oSaveCancelFooterStateOb)
    .do((oBtnState) => {
      const oModel = self.getModel("vmButtonsState");
      oModel.setProperty("/bSave", oBtnState.bSave);
      oModel.setProperty("/bCancel", oBtnState.bCancel)
    }).subscribe(
        undefined,
        onErr => jQuery.sap.log.fatal(oErr)
    );

If the order doesn't matter you can take both streams and use merge:

Rx.Observable.merge(oMassChangeOb, oSaveCancelFooterStateOb)
    .subscribe(
        undefined,
        onErr => jQuery.sap.log.fatal(oErr)
    );

As a general note: Try to move as much logic as you can into the stream itself(e.g. by using .do as I did above) and have the subscribe as clean and generic as possible, this will make it much easier to combine(merge, chain, ect..) multiple streams, e.g.:

let preparedOMassChangeOb = oMassChangeOb
    .do(function (aMassDates) {
      const oModel = self.getModel("vmCalSpecialDates");
      oModel.setProperty("/aActualDates", aMassDates);
    })
    .catch(..some error-handler-logic...);

let preparedOSaveCancelFooterStateOb = oSaveCancelFooterStateOb
    .do((oBtnState) => {
      const oModel = self.getModel("vmButtonsState");
      oModel.setProperty("/bSave", oBtnState.bSave);
      oModel.setProperty("/bCancel", oBtnState.bCancel)
    });

Rx.Observable.merge(
    preparedOMassChangeOb,
    preparedOSaveCancelFooterStateOb
).subscribe();