我有以下代码从Web服务中获取数据
@Effect()
searchAction$ : Observable<Action> = this._actions$
.ofType(ActionTypes.SEARCH_CASES)
.do(val => this._store.dispatch(new SetLoadingAction(true)))
.map(action => action.payload) //just interested in the payload
.map(payload => new CaseSearchCriteria(payload)) //form search criteria
.switchMap(payload => this._httpSearchCases.searchDiseaseCases(payload)
/*
1. Return from httpCall is IDiseaseControlCaseManagement[]
2. Transform this array into SearchDiseaseCaseResults[]
*/
.do((val) =>{
console.log('Type of Value: ', typeof val);
console.log('Is value SearchDiseaseCaseResult? :', val instanceof SearchDiseaseCaseResults);
})
.map(res => new LoadSearchResultsAction(res))
);
我添加了简短说明我所需功能的评论,我确定有一个ReactiveX操作符可以实现这一点,但我无法找到我正在寻找的内容。
我尝试推送到.scan
运算符的累加器
.scan((acc: SearchDiseaseCaseResults[], val: IDiseaseControlCaseManagement) => {
acc.push(new SearchDiseaseCaseResults(val));
})
然而,TS静态分析告诉我这是不正确的,
Error:(32, 31) TS2453:The type argument for type parameter 'R' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidate 'SearchDiseaseCaseResults[]' is not a valid type argument because it is not a supertype of candidate 'void'.
所以我需要一个允许我 的操作员链(或方法):
或
答案 0 :(得分:0)
除非我误解了您,否则您正在寻找的运营商只是普通的map
。
退一步,让我们专注于你的switchMap。
.switchMap(payload => this._httpSearchCases.searchDiseaseCases(payload)
// 1. Return from httpCall is IDiseaseControlCaseManagement[]
.map(IDiseaseControlCaseManagementArray => this.mapToSearchDiseaseCaseResultsArray(IDiseaseControlCaseManagementArray) )
.do((val) =>{
console.log('Type of Value: ', typeof val);
console.log('Is value SearchDiseaseCaseResult? :', val instanceof SearchDiseaseCaseResults);
})
.map(res => new LoadSearchResultsAction(res))
);
我认为这基本上就是你所要求的 - mapToSearchDiseaseCaseResultArray
方法的实现。
而且,如果你可以将单个IDiseaseControlCaseManagement
映射到单个SearchDiseaseCaseResult
,那么实际上,实现它的数组版本很简单。
因此,假设有一个方法convertFrom
可以像:
searchDiseaseCaseResult = convertFrom(iDeseaseControlCaseManagement)
然后,使用数组,
mapToSearchDiseaseCaseResultArray(sourceArray) {
return sourceArray.map(caseManagement => this.convertFrom(caseManagement));
}
当然,您仍然需要编写convertFrom
方法来转换单个项目,但是一旦完成,您可以将其插入上述方法。
以上是否有意义?