2016-04-08 12 views
5

初心者質問:私はngrxを使ってangular2アプリを持っています。コンポーネントに状態(オブザーバブルの配列)を返すサービスがあります。状態をフィルタリングする場所は?

私の質問は、そのコンポーネントの読み取り専用サブセットをコンポーネントで使用したい場合です。

私は減速機、サービス、またはコンポーネントで行いますか?

答えて

2

ガイダンスは、ngrx example applicationに記載されています。セレクタがalongside reducers定義されているパターンが存在する:

/** 
* Because the data structure is defined within the reducer it is optimal to 
* locate our selector functions at this level. If store is to be thought of 
* as a database, and reducers the tables, selectors can be considered the 
* queries into said database. Remember to keep your selectors small and 
* focused so they can be combined and composed to fit each particular 
* use-case. 
*/ 
export function getBookEntities() { 
    return (state$: Observable<BooksState>) => state$ 
    .select(s => s.entities); 
}; 

そして、それらのセレクタは、フィルタ/状態を選択するためにused in (smart) componentsです:

... 
export class CollectionPage { 

    books$: Observable<BooksInput>; 

    constructor(store: Store<AppState>) { 
    this.books$ = store.let(getBookCollection()); 
    } 
} 

このパターン/メカニズムは、いずれかの状態をフィルタリングするために使用することができコンポーネント、サービスのいずれかを選択できます。

+0

よろしくお願いします。 – user3253156