zoukankan      html  css  js  c++  java
  • [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 }
      );
  • 相关阅读:
    MySQL全文索引--转载
    提升接口tps
    数据库连接池了解和常用连接池对比
    SpringBoot跨域配置,解决跨域上传文件
    oss上传
    MySQL高级 之 explain
    spring cloud集群负载均衡
    Xmind日常操作
    产品经理应该懂点经济学
    初谈产品
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11638185.html
Copyright © 2011-2022 走看看