从一个轨道跳到另一个轨道

时间:2016-12-19 11:26:19

标签: javascript rxjs

我有以下观察:

  let oSelectOb = oSelect
    .map(function (oEvent) {
      return oEvent.getSource();
    })
    .map(function (oControl) {
      let oItem = oControl.getSelectedItem();
      let aKeys = oItem.getKey().split("/");
      return {plant: aKeys[0], wc: aKeys[1]};
    })
    .flatMap(function (oSelectedItem) {

      let oModel = self.getModel("weightProtocolService");
      let oPlantFilter = new Filter("Plant", sap.ui.model.FilterOperator.EQ, oSelectedItem.plant);
      let oWcFilter = new Filter("WorkCenter", sap.ui.model.FilterOperator.EQ, oSelectedItem.wc);

      return Rx.Observable.create(function (observer) {
        oModel.read("/CostCenterCalendarSet", {
          success: function (oData, oResponse) {
            observer.next(oResponse);
          },
          error: function (oError) {
            observer.error(oError);
          },
          filters: [oPlantFilter, oWcFilter]
        });
      });
    })
    .map(function (oData) {
      return oData.data.results;
    });

下一步,我想验证oData.data.results数组是否大于0,如果是,则应继续传递数组。 如果数组等于零,则应该采用其他可观察数据。

喜欢从一个轨道跳到另一个轨道,当出现问题时(如果/其他情况)。

更新

oSelect
   .map(function (oEvent) {
    // Clean up dates
    let oModel = self.getModel("vmCalSpecialDates");
    oModel.setProperty("/specialDates", []);

    return oEvent.getSource();
  })
  .map(function (oControl) {
    let oItem = oControl.getSelectedItem();
    let aKeys = oItem.getKey().split("/");
    return {plant: aKeys[0], wc: aKeys[1]};
  })
  .flatMap(function (oSelectedItem) {

    let oModel = self.getModel("weightProtocolService");
    let oPlantFilter = new Filter("Plant", sap.ui.model.FilterOperator.EQ, oSelectedItem.plant);
    let oWcFilter = new Filter("WorkCenter", sap.ui.model.FilterOperator.EQ, oSelectedItem.wc);

    return Rx.Observable.create(function (observer) {
      oModel.read("/CostCenterCalendarSet", {
        success: function (oData, oResponse) {
          observer.next(oResponse);
        },
        error: function (oError) {
          observer.error(oError);
        },
        filters: [oPlantFilter, oWcFilter]
      });
    });
  })
  .map(function (oData) {
    return oData.data.results;
  })
  .filter(function (aData) {
    return aData.length > 0;
  })
  .flatMap(function (aData) {
    return Rx.Observable.from(aData)
  })
  .filter(function (oData) {
    let oToday = new Date();
    return oData.InspectionDate.getTime() > oToday.getTime();
  });
正如你在第一个地图功能上看到的那样,我可以在那里进行清理:

// Clean up dates
let oModel = self.getModel("vmCalSpecialDates");
oModel.setProperty("/specialDates", []);

但我认为,这不是解决问题的干净方法。它打破了功能编程思想。

更新2
我尝试了如下:

  return oSelect
    .map(function (oEvent) {
      // Clean up dates
      /*let oModel = self.getModel("vmCalSpecialDates");
      oModel.setProperty("/specialDates", []);*/

      return oEvent.getSource();
    })
    .map(function (oControl) {
      let oItem = oControl.getSelectedItem();
      let aKeys = oItem.getKey().split("/");
      return {plant: aKeys[0], wc: aKeys[1]};
    })
    .flatMap(function (oSelectedItem) {

      let oModel = self.getModel("weightProtocolService");
      let oPlantFilter = new Filter("Plant", sap.ui.model.FilterOperator.EQ, oSelectedItem.plant);
      let oWcFilter = new Filter("WorkCenter", sap.ui.model.FilterOperator.EQ, oSelectedItem.wc);

      return Rx.Observable.create(function (observer) {
        oModel.read("/CostCenterCalendarSet", {
          success: function (oData, oResponse) {
            observer.next(oResponse);
          },
          error: function (oError) {
            observer.error(oError);
          },
          filters: [oPlantFilter, oWcFilter]
        });
      });
    })
    .map(function (oData) {
      return oData.data.results;
    })
    .flatMap(function (aData) {
      if (aData.length === 0) {
        return Rx.Observable.empty();
      }
      return Rx.Observable.from(aData)
    })
    .filter(function (oData) {
      let oToday = new Date();
      return oData.InspectionDate.getTime() > oToday.getTime();
    });

并且根本不会调用subscribe方法。

1 个答案:

答案 0 :(得分:0)

您所描述的内容看起来像典型的mergeMap()

.mergeMap(function(oData) {
    return oData.data.results.length > 0
       ? Observable.of(oData.data.results)
       : Observable.empty();
});