0
ngrx/store를 사용하는 Angular2 앱에서 API의 일부 데이터를 쿼리하고 저장소로 전달하는 메소드가 있습니다. 작동 중입니다. ok :Angular2 + ngrx/store : 루프 내에서 상태를 변경하는 방법은 무엇입니까?
get(id: number) {
return this.http.get(`http://localhost:3000/contents/${id}.json`).map(response => {
return response.json();
})
.map((content: Content) => {
this.store.dispatch({ type: 'GET_CONTENT', payload: content });
return content;
});
}
API를 쿼리하는 다른 서비스가 있는데 배열 객체를 반환합니다. 이 경우, 다음과 같이 처리합니다.
return this.http.get(`http://localhost:3000/contents/${id}/contents.json`).map(response => {
return response.json();
})
.map((contents: Array<Content>) => {
contents.map(payload => {
this.store.dispatch({ type: 'GET_CONTENT', payload });
});
return contents;
})
작동하지만 저장소 상태가 여러 번 변경됩니다. 더 나은 접근 방법이 있습니까? 어쩌면 매장의 모든 것을 동시에 처리하는 또 다른 액션 ('GET_CONTENTS')을 만들 수 있습니까? 또는 어떻게 든 'contents.map'이 끝난 후에 상태를 변경해야한다고 말할 수 있습니까?
상태가 제대로 작동하고 함수가 순수에 대한 순수한 기능을해야 하는가? –