2017-04-06 49 views
1

我已經建立了這個IStoreNGRX:結合到選擇

export interface IStore { 
    user: IUser; 
    sources: ISourceRedux; 
} 

其中IUser是:

export interface IUser { 
    id: string; 
    cname: string; 
    sname: string; 
    ... 
} 

ISourceRedux是:

export interface ISourceRedux { 
    entities: { [key: string]: ISource }; 
    ids: Array<string>; 
    selectedIds: Array<string>; 
    editingSource: ISource; 
    defaultId: string; 
} 

所以,我創建這些選擇器:

export const getSourcesState = (state: IStore) => state.sources; 
export const getSelectedIds = (sourceRdx: ISourceRedux) => sourceRdx.selectedIds; 
export const getSelectedSourceIds = createSelector(getSourcesState, fromSources.getSelectedIds); 

所以,到現在爲止,以檢查是否有用戶登錄我這樣做:

this.store$ 
    .select(fromRoot.getUserState) 
    .filter(user => user.id != null && user.logged) 
    .do(user => this.store$.dispatch(...)) 
    ... 

現在我strugling用於獲取用戶信息和selectedSourceIds在同一時間,以便檢查如果:

  1. 用戶登錄(this.store$.select(fromRoot.getUserState)
  2. 然後讓所有selectedSourceIds(this.store.select(fromRoot.getSelectedSourceIds)
  3. 調度一個動作

我怎麼能得到這個?

+0

看看'withLatestFrom' RxJs運營商 – Maxime

回答

2

這是我的解決方案:

this.store$.combineLatest(
     this.store$.select(fromRoot.getUserEntity), 
     this.store$.select(fromRoot.getSelectedSourceIds), 
     (store, user, selectedSourceIds) => ({user: user, selectedSourceIds: selectedSourceIds}) 
    ) 
    .filter((proj) => proj.user.id != null && proj.user.logged) 
    .do((proj) => this.store$.dispatch({type: 'DELETE_CARDS', payload: {username: proj.user.username, tokens: proj.selectedSourceIds}})) 
    .take(1) 
    .subscribe(); 

我希望它是有用的。

+0

我認爲這種方法沒有利用重新選擇(memoized選擇器)的解釋與此處的結果https://netbasal.com/lets-talk-about-select-and -reselect功能於NGRX店,177a2f6045a8 –