Request Action监听

在Request Action中,有3个事件可以被其它模型监听onPrepare, onSuccess, onFail

  1. // ForthModel.ts
  2. import { Model, Effects } from '@redux-model/web';
  3. import { thirdModel } from './ThirdModel';
  4.  
  5. interface Data {
  6. status: string;
  7. }
  8.  
  9. class ForthModel extends Model<Data> {
  10. // 监听其他模型的Action
  11. protected effects(): Effects<Data> {
  12. return [
  13. thirdModel.increase.onPrepare((state) => {
  14. state.status = 'prepare';
  15. }),
  16.  
  17. thirdModel.increase.onSuccess((state) => {
  18. state.status = 'success';
  19. }),
  20.  
  21. thirdModel.increase.onFail((state) => {
  22. state.status = 'fail';
  23. }),
  24.  
  25. ];
  26. }
  27.  
  28. protected initReducer(): Data {
  29. return {
  30. status: '',
  31. };
  32. }
  33. }
  34.  
  35. export const forthModel = new ForthModel();

我们使用effects()方法监听其他模型的Action的调用,并同步更改当前模型的reducer数据。

  1. console.log(forthModel.data.status) // status === ''
  2.  
  3. const promise = thirdModel.getProfile(2);
  4. console.log(forthModel.data.status) // status === 'prepare'
  5.  
  6. promise
  7. .then(() => {
  8. console.log(forthModel.data.status) // status === 'success'
  9. })
  10. .catch(() => {
  11. console.log(forthModel.data.status) // status === 'fail'
  12. });