2017-08-07 85 views
0

我試圖沒有任何成功鏈鏈操縱使用lodash鏈和承諾。我的refColors.length始終爲0,我從不輸入if語句,因爲服務在語句測試後被調用。使用lodash鏈承諾同步數據

我試圖在我的功能做

initiateModalData(refColorList: any) { 

    refColorList.forEach((values, index) => { 
     const listRefRows: Array<GblRowValues> = []; 
     const initialcolors: Array<any> = []; 
     const refColors: Array<any> = []; 
     const headList: Array<any> = []; 
     this.rowId = 1; 

     _(value) 
       .chain() 
       .tap((colors) => { 
        this.myAPIService.getStyle(colors[index].id) 
         .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }); 
       }) 
       .forEach(refSize => { 
        refSize.headList.forEach(item => { 
         headList.push({ 
          size: item.size, 
          value: 0 
         }); 
        }); 
       }) 
       .forEach(val => { 
        initialcolors.push(val.color); 
       }) 
       .forEach((row) => { 
        this.rowId += 1; 
        listRefRows.push(new rowValue(row.headList, initialcolors, this.rowId)); 
       }) 
       .value(); 

      const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
      const ref = this.target.createComponent(factory); 
      console.log(refColors.length) 
      ref.instance.listRows = listRefRows; 
      ref.instance.rowId = this.rowId; 
      ref.instance.headList = _.uniqBy(headList, 'size'); 
      if (_.difference(refColors, initialcolors).length !== 0) { 
       ref.instance.colors = _.difference(refColors, initialcolors); 
       ref.instance.isAddRowValid = true; 
      } 
    }); 
    } 

我試圖把所有的鏈部分在一個新的承諾,並在當時的()我開始我的componant但我componant而無數據

啓動
return new Promise((resolve, reject) => { 
     _(value) 
       .chain() 
       .tap((colors) => { 
        this.myAPIService.getStyle(colors[index].id) 
         .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }); 
       }) 
       .forEach(refSize => { 
        refSize.headList.forEach(item => { 
         headList.push({ 
          size: item.size, 
          value: 0 
         }); 
        }); 
       }) 
       .forEach(val => { 
        initialcolors.push(val.color); 
       }) 
       .forEach((row) => { 
        this.rowId += 1; 
        listRefRows.push(new rowValue(row.headList, initialcolors, this.rowId)); 
       }) 
       .value(); 

}) 
    .then (() => { 
    const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
      const ref = this.target.createComponent(factory); 
      console.log(refColors.length) 
      ref.instance.listRows = listRefRows; 
      ref.instance.rowId = this.rowId; 
      ref.instance.headList = _.uniqBy(headList, 'size'); 
      if (_.difference(refColors, initialcolors).length !== 0) { 
       ref.instance.colors = _.difference(refColors, initialcolors); 
       ref.instance.isAddRowValid = true; 
      } 
    }) 

回答

0

我解決我的問題,通過啓動我的componant後的訂閱和使用最後的forEach後的自來水()是像這樣執行:

   .tap((colors) => { 
       this.myAPIService.getStyle(colors[index].id) 
        .subscribe(styleRef => { 
          if (styleRef) { 
           styleRef.colors.map(color => { 
            refColors.push(color.colorId); 
           }); 
          } 
         }, 
          error => this.myNotificationService.error(`${error} !`, 'error'), 
         () => { 
          const factory = this.componentFactoryResolver.resolveComponentFactory(ReferenceTableComponent); 
          const ref = this.target.createComponent(factory); 
          ref.instance.listRows = listRefRows; 
          ref.instance.rowId = this.rowId; 
          ref.instance.headList = _.uniqBy(headList, 'size'); 
          if (_.difference(refColors, initialcolors).length !== 0) { 
           ref.instance.colors = _.difference(refColors, initialcolors); 
          } 
         } 
        ); 
      })