0

Need help to invoke side-effect actions, while my item saving in Firebase.

vehicle.service.ts:

...

public addVehicle(vehicle: Vehicle, success, error) {
  return this.vehicles$.push(vehicle).then(success).catch(error);
}

vehicle.effects.ts:

    @Effect()
    addVehicle$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE)
    // NEED TO CALL: this._statusActions.dataLoading('adding new vehicle...')) 
    .switchMap(action => 
        Observable.fromPromise(
            this._vehicleService.addVehicle(action.payload,
                suc => this._statusActions.dataAdded(action.payload.name),
                err => this._statusActions.dataNotSaved(action.payload.name)
            ) // I WANT TO CALL, NOT RETURN ONE OF THEM
    ))
4

1 回答 1

1

添加中间操作

 @Effect()
    addVehicleStatusChange$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE)
    .switchMap(this._statusActions.dataLoading('adding new vehicle...')) 
    // return action type ADD_VEHICLE_START


 @Effect()
    addVehicle$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_START)
    .switchMap(action => 
        Observable.fromPromise(
            this._vehicleService.addVehicle(action.payload,
                // Success return action ADD_VEHICLE_SUCCESS
                // Error return ADD_VEHICLE_ERROR
           ) 
    ))

 @Effect()
    addVehicleStatusChangeSuccess$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_SUCCESS)
     .switchMap(suc => this._statusActions.dataAdded(action.payload.name)),
    // return action type some random action you don't care 

 @Effect()
    addVehicleStatusChangeError$ = this.actions$
    .ofType(VehicleActions.ADD_VEHICLE_ERROR)
     .switchMap( =err => this._statusActions.dataNotSaved(action.payload.name))
    // Return some random action. 

我通常做的是从组件中调度 PERFORM_ACTION。在效果中监听该动作并返回新动作 PERFORM_ACTION_START。

一旦动作完成,返回动作PERFORM_ACTION_DONE。

在减速器内部,我只有 PERFORM_ACTION_DONE 的处理程序。我有特殊的状态减速器,在那里我听 PERFORM_ACTION_START 和 PERFORM_ACTION_DONE

于 2017-06-15T01:25:42.180 回答