在调用Web服务之前,我的服务类需要从状态中获取一个名为 dataForUpdate
的属性。目前,我是这样做的:
constructor(public _store: Store < AppState > ,
public _APIService: APIService) {
const store$ = this._store.select('StateReducer');
.../...
let update = this.actions$.filter(action => action.type == UPDATE)
.do((action) => this._store.dispatch({
type: REDUCER_UPDATING,
payload: action.payload
})) **
* GET STATE ** *= => .mergeMap(action => store$.map((state: AppState) => state.dataForUpdate).distinctUntilChanged(),
(action, dataForUpdate) {
return {
type: action.type,
payload: {
employee: action.payload,
dataForUpdate: dataForUpdate
}
};
}) *
AND CALL API *= => .mergeMap(action => this._APIService.updateEmployee(action.payload.employee, action.payload.dataForUpdate),
(action, APIResult) => {
return {
type: REDUCER_UPDATED
}
})
.share();
.../...
let all = Observable.merge(update, ....);
all.subscribe((action: Action) => this._store.dispatch(action));
}
我正在使用angular2-store-example (https://github.com/ngrx/angular2-store-example/blob/master/src/app/users/models/users.ts)作为指南。
我想知道是否存在更好(更清晰)的方法?
实时示例:https://plnkr.co/edit/WRPfMzPolQsYNGzBUS1g?p=preview