NGRX实体:如何将action.payload分配给UpsertOne中的状态属性

时间:2018-11-01 19:36:10

标签: angular ngrx ngrx-store ngrx-entity

我有一个处于实体状态的减速器。在执行action.payload.Message操作saveMessage.msg的同时如何将upsertOne分配给UPSERT_Message_SUCCESS

 export interface MessageState extends EntityState<Message> {
  // additional entities state properties
    selectedMessageId: number | null;
    loaded: boolean;
    loading: boolean;
  saveMessage?: {
    loaded: boolean;
    loading: boolean;
    msg?: Message;
  };
}

export const adapter: EntityAdapter<Message> = createEntityAdapter<Message>({
  selectId: (msg: Message) => msg.messageId,

});

export const initialState: MessageState = adapter.getInitialState({
  // additional entity state properties
    selectedMessageId: null,
    loaded: false,
    loading: false,
  saveMessage: {
    loaded: false,
    loading: false
  },

});

export function reducer(state = initialState, action: MessageActionsUnion): MessageState {
  switch (action.type) {

    case MessageActionTypes.UPSERT_MessageS_SUCCESS: {
      return adapter.upsertMany(action.payload.Messages, { ...state, loaded: true, loading: false });
    }



    case MessageActionTypes.UPSERT_Message: {
      return { ...state,loading: true, loaded:false };
    }


   //assign action.payload.message to saveMessage.msg

    case MessageActionTypes.UPSERT_Message_SUCCESS: {
   return  adapter.upsertOne(action.payload.saveMessageHeader, {...state,loaded:true,loading:false});

  // return {
  //   ...state,
  //   saveMessage: {
  //     loading: false,
  //     loaded: true,
  //     msgHeader: action.payload.saveMessageHeader
  //   }
 // };
}
    case MessageActionTypes.UPSERT_Message_FAIL: {
      return { ...state };
    }



    default: {
      return state;
    }
  }
}

相应的动作调用签名如下:

export class SaveMessageSuccess implements Action {
  readonly type = MessageActionTypes.SAVE_MESSAGE_SUCCESS;
  constructor(public payload:  {saveMessage:partial <Message> }) {
  }
}

1 个答案:

答案 0 :(得分:0)

clear all %plot1 %data M1=[0.000000 1188.000000 340.216815 6.500000 1188.500000 331.115127 13.000000 1189.000000 325.858265 19.500000 1189.000000 330.567837 26.000000 1190.000000 333.606362 32.500000 1191.000000 329.369873 45.500000 1192.500000 315.677226 52.000000 1193.000000 316.240698 58.500000 1193.500000 330.965579 65.000000 1194.000000 341.223389 71.500000 1195.000000 332.790136 84.500000 1156.616455 2099.999905 91.000000 1204.000000 310.592890 104.000000 1160.572510 2099.999905 110.500000 1207.500000 305.550069 117.000000 1163.173218 2099.999905 123.500000 1210.500000 310.125440 130.000000 1165.852295 2099.999905 136.500000 1213.500000 313.500583 143.000000 1168.562500 2099.999905]; %upper limit h1=[0.0 1188.0 6.5 1188.5 13.0 1189.0 19.5 1189.0 26.0 1190.0 32.5 1191.0 39.0 1192.0 45.5 1192.5 52.0 1193.0 58.5 1193.5 65.0 1194.0 71.5 1195.0 78.0 1198.0 84.5 1202.5 91.0 1204.0 97.5 1205.0 104.0 1206.5 110.5 1207.5 117.0 1208.0 123.5 1210.5 130.0 1212.0 136.5 1213.5 143.0 1217.0 149.5 1218.5]; %plot2 %data M2=[0.000000 1217.000000 594.503284 4.500000 1183.886353 2099.999905 9.000000 1220.000000 1071.599126 13.500000 1184.565430 2099.999905 18.000000 1219.000000 435.631812 22.500000 1185.150635 2099.999905 24.500000 1217.555542 320.541441 27.000000 1185.427490 2099.999905 31.500000 1216.000000 300.000012 36.000000 1185.981445 2099.999905 40.500000 1215.000000 306.778669 45.000000 1186.629272 2099.999905 49.500000 1216.000000 300.000012 54.000000 1187.214478 2099.999905 58.500000 1215.000000 300.000012 63.000000 1187.893555 2099.999905 67.500000 1218.000000 335.902870 72.000000 1188.572510 2099.999905 76.500000 1220.000000 359.615386 81.000000 1189.282715 2099.999905 85.500000 1224.000000 1382.480264 90.000000 1189.992920 2099.999905 94.500000 1225.000000 1206.023455 99.000000 1190.578125 2099.999905]; %upper limit h2=[0.0 1217 4.5 1217 9.0 1220 13.5 1219 18.0 1219 22.5 1218 27.0 1217 31.5 1216 36.0 1215 40.5 1215 45.0 1216 49.5 1216 54.0 1215 58.5 1215 63.0 1217 67.5 1218 72.0 1219 76.5 1220 81.0 1222 85.5 1224 90.0 1225 94.5 1225 99.0 1224 103.5 1225]; %% figure (1) X1=M1(:,1); Y1=M1(:,2); Z1=M1(:,3); [x1,y1]=meshgrid(linspace(min(X1),max(X1),100),linspace(min(Y1),max(Y1),100)); z1=griddata(X1,Y1,Z1,x1(:),y1(:)); [c,h]=contourf(x1,y1,reshape(z1,100,100)); hold on plot(h1(:,1),h1(:,2),'k','linewidth',4) %upper limit %% figure (2) X2=M2(:,1); Y2=M2(:,2); Z2=M2(:,3); [x2,y2]=meshgrid(linspace(min(X2),max(X2),100),linspace(min(Y2),max(Y2),100)); z2=griddata(X2,Y2,Z2,x2(:),y2(:)); [c,h]=contourf(x2,y2,reshape(z2,100,100)); hold on plot(h2(:,1),h2(:,2),'k','linewidth',4) %upper limit 不允许upsertOne,但需要实体Partial<Entity>

在这种情况下,将动作创建者更改为以下内容可以解决问题:

T

@ngrx/entity docs