我正在学习Angular 2,并尝试使用ngrx/store,但在某些特殊情况下遇到了一些困难。
例如,我想删除一个父对象。我想做的是同时删除子对象。
以下是我的实体:
export class Discussion {
id: string;
name: string;
createdAt: Date;
posts: Post[];
}
export class Post {
id: string;
title: string;
data: string;
createdAt: Date;
comments: Comment[];
}
export class Comment {
id: string;
data: string;
createdAt: Date;
}
我正在使用normalizr来扁平化我的状态,以便我的存储讨论看起来像这样:
{
id: "1",
name: "First dicussion",
createdAt: "...",
posts: ["1", "2", "3", "5"]
}
我有三个reducers,一个用于讨论(Discussions),另一个用于帖子(Posts),最后一个用于评论(Comments)。所有的reducers都处理它们自己类型的删除操作。以下是讨论(Discussion)reducer的示例:
export function reducer(state = initialState, action: discussion.Actions): State {
switch (action.type) {
case discussion.REMOVE: {
const idToRemove = action.payload;
const newEntities = state.entities;
delete newEntities[idToRemove];
return Object.assign({}, state, {
entities: newEntities
});
}
}}
我的行动看起来像这样:
export class RemoveAction implements Action {
readonly type = REMOVE;
/**
* Constructor
* @param payload The id of the discussion to remove
*/
constructor(public payload: string) { }
}
当我删除一次讨论时,我希望删除与该讨论相关的帖子,并且影响将删除与已删除帖子相关的评论。为了做到这一点,我使用了ngrx的效果,因此我使用了以下效果:
@Effect()
removeDiscussion: Observable<Action> = this._actions
.ofType(dicussion.REMOVE)
.map((action: discussion.RemoveAction) => action.payload)
.mergeMap(discId => {
// How to get posts from discussion id ???
// Fire related Actions
return [
new posts.RemoveAction(postsToRemove)
];
});
我的问题是如何从讨论的ID中删除帖子?
谢谢阅读。
import'rxjs/add/operator/withLatestFrom';
。由于我是初学者,我忘记了添加它。 - Lopeur