我有一些以下的应用程序状态树:
AppState
{
    MyPage1 - MyList - MyItem[]
    MyPage2 - MySubPage - MyList - MyItem[]
    MyPage3 - MyItem   //can be stand-alone as well
}

所以,我有一个页面上的项目列表.我通过将操作分派给MyList reducer来加载此列表.加载项后,它们将作为输入传递给MyListComponent,从而创建具有相应输入的MyItemComponents.
MyItemComponent也可以是独立的.因此,它并不总是依赖于MyList.

问题是每个MyItem都有自己的动作(例如可以被喜欢或编辑).而且我现在已经陷入困境,在那种情况下我应该采取什么行动.如何识别哪个项目已更新以及应更新哪个列表.

当用户更新列表中的某个项目时,操作流程是什么?

而且 – 如果我直接更改其中一个MyItem,是否会检测到MyList的状态更改?

我可能会想到的情况是(在MyItem本身上有所有适当的操作)向MyList添加额外的操作 – 例如:

updateItem(payload: 
    {
        updateType,//e.g. like,edit,delete
        data,//data needed to make an update
        itemId      //index in the MyItem array
    })

然后,我以某种方式从MyList向数组中指定的MyItem触发另一个动作,item触发相应的动作来更新自己的状态(通过@Effect更新)
如果更新成功,则会触发新操作以更新列表.

说实话,这一切看起来都非常冗长和含糊.甚至不确定它将如何在实践中实施.真的需要有经验的人的建议.

附:
我应该提到所有数据都是从REST api获取并发回的.

编辑:
如果遵循Comprehensive Introduction to @ngrx/store的方法,应用程序将有重复操作的音调(每个容器 – MyList – 将重复MyItem的所有操作).我的案子会有更合适的方法吗?

这样做的方法是使用一个子reducer来处理状态树的子部分.

如果您需要将操作发送到阵列中的单个项目,则将所有这些操作传递给处理单个项目的子缩减器.

这是一个有评论的新闻项目的例子.
我希望它可以清除一些东西.

import { Action } from '@ngrx/store';
import { AppState } from '../reducer';
import { NewsItem } from './news.model';
import * as newsActions from './news.actions';

export interface Newsstate {
  readonly loading: boolean;
  readonly entities: NewsItem[];
}

export interface AppStateWithNews extends AppState {
  readonly news: Newsstate;
}

const initialState: Newsstate = {
  loading: false,entities: [],};

const newsItemReducer = (newsItem: NewsItem,action: newsActions.NewsActionTypes): NewsItem => {

  switch (action.type) {
    case newsActions.Types.UPDATE:
    case newsActions.Types.REMOVE:
    case newsActions.Types.COMMENT_CREATE:
    case newsActions.Types.COMMENT_REMOVE:
      return Object.assign({},newsItem,{
        action: true
      });

    case newsActions.Types.UPDATE_SUCCESS:
      return Object.assign({},action.payload,{
        action: false
      });

    case newsActions.Types.COMMENT_CREATE_SUCCESS:
      return Object.assign({},{
        action: false,comments: [action.payload.comment,...newsItem.comments]
      });

    case newsActions.Types.COMMENT_REMOVE_SUCCESS:
      return Object.assign({},comments: newsItem.comments.filter(i => i.id !== action.payload.commentId)
      });

    default:
      return newsItem;
  }
};

export const newsReducer = (state: Newsstate = initialState,action: newsActions.NewsActionTypes): Newsstate => {

  switch (action.type) {
    case newsActions.Types.LIST:
      return Object.assign({},state,{ loading: true });

    case newsActions.Types.LIST_SUCCESS:
      return {
        loading: false,entities: action.payload
      };

    case newsActions.Types.CREATE_SUCCESS:
      return Object.assign({
        loading: false,entities: [action.payload,...state.entities]
      });

    case newsActions.Types.REMOVE_SUCCESS:
      return Object.assign({
        loading: false,entities: state.entities.filter(newsItem => newsItem !== action.payload)
      });

    // Delegate to newsItemReducer
    case newsActions.Types.UPDATE:
    case newsActions.Types.UPDATE_SUCCESS:
    case newsActions.Types.COMMENT_CREATE:
    case newsActions.Types.COMMENT_CREATE_SUCCESS:
    case newsActions.Types.COMMENT_REMOVE:
    case newsActions.Types.COMMENT_REMOVE_SUCCESS:
      return Object.assign({},{
        entities: state.entities.map(newsItem => {
          // Only call sub reducer if the incoming actions id matches
          if (newsItem.id === (action.payload.newsItem || action.payload).id) {
            return newsItemReducer(newsItem,action);
          }
          return newsItem;
        })
      });

    default:
      return state;
  }

};

并且看看如何在这里调用这些是news.actions.

import { Action } from '@ngrx/Store';
import { NewsItem } from './news.model';
import { Response } from '@angular/http';

export const Types = {
  LIST: '[News] List',LIST_SUCCESS: '[News] List Success',LIST_ERROR: '[News] List ERROR',CREATE: '[News] Create',CREATE_SUCCESS: '[News] Create Success',CREATE_ERROR: '[News] Create Error',UPDATE: '[News] Update',UPDATE_SUCCESS: '[News] Update Success',UPDATE_ERROR: '[News] Update Error',REMOVE: '[News] Remove',REMOVE_SUCCESS: '[News] Remove Success',REMOVE_ERROR: '[News] Remove Error',COMMENT_CREATE: '[News] Comment Create',COMMENT_CREATE_SUCCESS: '[News] Comment Create Success',COMMENT_CREATE_ERROR: '[News] Comment Create Error',COMMENT_REMOVE: '[News] Comment Remove',COMMENT_REMOVE_SUCCESS: '[News] Comment Remove Success',COMMENT_REMOVE_ERROR: '[News] Comment Remove Error',};

/**
 * List
 */
export class List implements Action {
  type = Types.LIST;
  constructor(public payload?: any) {}
}
export class ListSuccess implements Action {
  type = Types.LIST_SUCCESS;
  constructor(public payload: NewsItem[]) {}
}
export class ListError implements Action {
  type = Types.LIST_ERROR;
  constructor(public payload: Response) {}
}

/**
 * Create
 */
export class Create implements Action {
  type = Types.CREATE;
  constructor(public payload: NewsItem) {}
}
export class CreateSuccess implements Action {
  type = Types.CREATE_SUCCESS;
  constructor(public payload: NewsItem) {}
}
export class CreateError implements Action {
  type = Types.CREATE_ERROR;
  constructor(public payload: Response) {}
}

/**
 * Update
 */
export class Update implements Action {
  type = Types.UPDATE;
  constructor(public payload: NewsItem) {}
}
export class UpdateSuccess implements Action {
  type = Types.UPDATE_SUCCESS;
  constructor(public payload: NewsItem) {}
}
export class UpdateError implements Action {
  type = Types.UPDATE_ERROR;
  constructor(public payload: Response) {}
}

/**
 * Remove
 */
export class Remove implements Action {
  type = Types.REMOVE;
  constructor(public payload: NewsItem) {}
}
export class RemoveSuccess implements Action {
  type = Types.REMOVE_SUCCESS;
  constructor(public payload: NewsItem) {}
}
export class RemoveError implements Action {
  type = Types.REMOVE_ERROR;
  constructor(public payload: Response) {}
}

/**
 * Create Comment
 */
export class CommentCreate implements Action {
  type = Types.COMMENT_CREATE;
  payload: {
    newsItem: NewsItem,comment: string
  };

  constructor(newsItem: NewsItem,comment: string) {
    this.payload = {
      newsItem,comment
    };
  }
}
export class CommentCreateSuccess implements Action {
  type = Types.COMMENT_CREATE_SUCCESS;
  payload: {
    newsItem: NewsItem,comment: string
  };
  constructor(newsItem: NewsItem,comment
    };
  }
}
export class CommentCreateError implements Action {
  type = Types.COMMENT_CREATE_ERROR;
  constructor(public payload: Response) {}
}

/**
 * Remove Comment
 */
export class CommentRemove implements Action {
  type = Types.COMMENT_REMOVE;
  payload: {
    newsItem: NewsItem,commentId: string
  };

  constructor(newsItem: NewsItem,commentId: string) {
    this.payload = {
      newsItem,commentId
    };
  }
}
export class CommentRemoveSuccess implements Action {
  type = Types.COMMENT_REMOVE_SUCCESS;
  payload: {
    newsItem: NewsItem,commentId: string
  };
  constructor(newsItem: NewsItem,commentId
    };
  }
}
export class CommentRemoveError implements Action {
  type = Types.COMMENT_REMOVE_ERROR;
  constructor(public payload: Response) {}
}

export type NewsActionTypes =
    List
  | ListSuccess
  | ListError
  | Create
  | CreateSuccess
  | CreateError
  | Update
  | UpdateSuccess
  | UpdateError
  | Remove
  | RemoveSuccess
  | RemoveError
  | CommentCreate
  | CommentCreateSuccess
  | CommentCreateError
  | CommentRemove
  | CommentRemoveSuccess
  | CommentRemoveError;

angular – ngrx – 更新项目列表中的单个项目的更多相关文章

  1. 前端监听websocket消息并实时弹出(实例代码)

    这篇文章主要介绍了前端监听websocket消息并实时弹出,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. ios – 错误:’无法为keyPath新闻添加映射,一个已经存在…’

    解决方法新闻keyPath有两个映射:和删除第一个,看看它是否有帮助.干杯.

  3. ios – 如何在Xcode 4.5中完成代码签名

    解决方法事实证明,从AppStore升级XCode并不会自动升级所有内容.转到首选项/下载,并确保安装最新的命令行工具.

  4. Android Studio是否支持用于Android UI设计的AngularJS?

    我对AndroidStudio有疑问:AS在设计XML文件时是否支持AngularJS代码,例如:对于小动画或效果?

  5. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  6. 如何在android中对这行LayoutInflater.from()进行单元测试

    嗨我建立一个简单的RecycleView适配器,我试图测试适配器的所有方法,但onCreateViewHolder对我来说是困难的.我尝试使用mockito构建一个viewGroup类的模拟,并在调用getContext()时返回MockContext的间谍,但似乎是因为我返回一个MockContext,LayoutInflater.from()返回一个空指针异常.这是我的考验提前致谢.解决方法当

  7. android – RecyclerView.ViewHolder的部分更新

    我正在尝试在RecyclerView中随时更改该字段.每个ViewHolder单独包含一个CardView和其他几个视图.我想要动画的唯一视图是随着时间的推移.你可以看到,没有动画:逐个更新项目并不有帮助,因为整个CardView闪烁:有没有办法获取所有ViewHolders的列表,然后更新(动画)只是一个TextView里面的每一个?

  8. 利用require.js与angular搭建spa应用的方法实例

    这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。

  9. 详解Angular动态组件

    本文主要介绍了Angular动态组件,对此感兴趣的同学,可以亲自实验一下。

  10. 详解如何使用webpack+es6开发angular1.x

    本篇文章主要介绍了详解如何使用webpack+es6开发angular1.x,具有一定的参考价值,有兴趣的可以了解一下

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部