将Observable转换为Promise以将数据返回到网格

时间:2017-09-25 09:32:23

标签: angular angular2-directives devextreme

我有一个返回Observable的服务,我将其转换为Promise并寻找映射到将向网格显示数据的商店。

获取摘要详情的服务

getSummaries(year: String): Observable<Response> {
    return this.http.get(this.appUrl + '/summary?year=' + year, {headers: this.headers});
  }

我有以下组件调用服务来返回我希望在网格中填充的详细信息。我的网格当前为空,并且未显示该服务的任何详细信息。

组件

  export class AppComponent {
    dataSource: any;

   constructor(myService: SummaryService) {
    this.dataSource = {
          load: (loadOptions: any) => {
      return myService.getSummary(this.selectedYear)
        .toPromise()
        .then(details => { this.Summaries =  details.json()})
        .catch(error => { this.msgService.showError(this.msgs, "Error while getting Summary for year " + this.selectedYear )})
},
      fields: [{
        caption: 'Month End',
        width: 120,
        dataField: 'monthEndDate',
        area: 'row'
      },{
        caption: 'Company',
        dataField: 'company',
        width: 150,
        area: 'row'    
      }, {
        caption: 'Department',
        dataField: 'department',
        width: 150,
        area: 'row'
      }, {
        caption: 'Location',
        dataField: 'location',
        width: 150,
        area: 'row'
      }, {
        caption: 'Commodity',
        dataField: 'commodity',
        width: 150,
        area: 'row'
      },{
        caption: 'Inventory',
        dataField: 'inventoryCount',
        dataType: 'number',
        summaryType: 'sum',
        format: 'decimal',
        area: 'data',
        width: 100
      },{
        caption: 'Breaks',
        dataField: 'breakCount',
        dataType: 'number',
        summaryType: 'sum',
        format: 'decimal',
        area: 'data',
        width: 100
      },{
        caption: 'Approved',
        dataField: 'approvedCount',
        dataType: 'number',
        summaryType: 'sum',
        format: 'decimal',
        area: 'data',
        width: 100,
      }],
      store: this.summaries
    }
  }
}

0 个答案:

没有答案