• [NgRx] Optimistically Editing Entity Data


    First thing first, let's define a action to update entity:

    import { createAction, props } from "@ngrx/store";
    import { Update } from "@ngrx/entity";
    import { Course } from "./model/course";
    
    export const courseUpdated = createAction(
      "[Edit Course Dialog] Course Updated",
      props<{ update: Update<Course> }>()
    );

    The 'Update' interface has props: 'id, changes'.

    Component usage:

      onSave() {
        const course: Course = {
          ...this.course,
          ...this.form.value
        };
    
        const update: Update<Course> = {
          id: course.id,
          changes: course
        };
    
        this.store.dispatch(courseUpdated({ update }));
    
        this.dialogRef.close();
      }

    Reducer:

    export const coursesReducer = createReducer(
      initCoursesState,
    
      on(CoursesAction.courseUpdated, (state, action) =>
        adapter.updateOne(action.update, state)
      )
    );

    Effect:

      saveCourse$ = createEffect(
        () =>
          this.action$.pipe(
            ofType(CoursesAction.courseUpdated),
            concatMap(action =>
              this.coursesHttpService.saveCourse(
                action.update.id,
                action.update.changes
              )
            )
          ),
        { dispatch: false }
      );
  • 相关阅读:
    父组件和子组件之间的生命周期执行顺序
    Vue生命周期四个阶段
    Vue watch监听
    Vue computed计算属性 理解
    Vue中v-if 与 v-show的区别
    虚拟DOM
    MVC与MVVM原理
    单例模式 回流与重绘
    判断某个对象属于哪种数据类型
    原生JS的兼容性问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11638185.html
Copyright © 2020-2023  润新知