ngrx

how to mock ngrx selector in a component

烈酒焚心 提交于 2019-12-05 00:09:58
In a component, we use a ngrx selector to retrieve different parts of the state. public isListLoading$ = this.store.select(fromStore.getLoading); public users$ = this.store.select(fromStore.getUsers); the fromStore.method is created using ngrx createSelector method. For example: export const getState = createFeatureSelector<UsersState>('users'); export const getLoading = createSelector( getState, (state: UsersState) => state.loading ); I use these observables in the template: <div class="loader" *ngIf="isLoading$ | async"></div> <ul class="userList"> <li class="userItem" *ngFor="let user of

Angular 4 resolve not completed when using ngrx

有些话、适合烂在心里 提交于 2019-12-04 23:59:41
问题 I'm trying to use ngrx in a resolve in my app and for some reason it's not working. This is how I got it previously, using a simple service in my resolve route: resolve() { return this.service .getData() .map(data => data.pages.filter(page => page.parent === 'home')); } I then changed it into this: resolve() { this.store.dispatch(new LoadConfigAction()); return this.store .select('configuration') .do(data => console.log(data)) .map((data: any) => data.pages.filter(page => page.parent ===

Observable async vs sync

自闭症网瘾萝莉.ら 提交于 2019-12-04 22:48:40
问题 How do I know when an Observable producer is async or sync? An sync example: Observable.of([1, 2, 3]) another async example (ngrx Store, see here) this.store.take(1); And now an obvious async example: this.http.get(restUrl) I fully understand how this works, and that some Observables can be sync and others Async. What I don't understand is how i can tell the difference in advance? Is there for example an obvious interface on the producer that tells me that it will be async? tl;dr The main

Infinite loop with store and http with ngFor

↘锁芯ラ 提交于 2019-12-04 20:15:31
I have infinite loop with this code: Template: <ng-container *ngFor="let row of service.dataLoadAsync() | async"> Service: dataLoadAsync(filters: FinishedCallsFilter = {}): Observable<FinishedCall[]> { return this.httpFinishedCallsObservable(Object.assign({limit: this.limit}, {})).flatMap(response => { this.store.dispatch(new ReplaceMany(response.items)) return this.store }) } Also I tried this function realization: dataLoadAsync(filters: FinishedCallsFilter = {}): Observable<FinishedCall[]> { this.httpFinishedCallsObservable(Object.assign({limit: this.limit}, {})) .subscribe(response => this

ngrx effect not being called when action is dispatched from component

ε祈祈猫儿з 提交于 2019-12-04 16:22:48
问题 I am having an issue with the ngrx store not dispatching an action to the effect supposed to deal with it. Here is the component that tries to dispatch: signin() { this.formStatus.submitted = true; if (this.formStatus.form.valid) { this.store.dispatch(new StandardSigninAction(this.formStatus.form.value.credentials)); } } The actions: export const ActionTypes = { STANDARD_SIGNIN: type('[Session] Standard Signin'), LOAD_PERSONAL_INFO: type('[Session] Load Personal Info'), LOAD_USER_ACCOUNT:

Angular router guard and ROUTER_NAVIGATION effect order

房东的猫 提交于 2019-12-04 11:44:29
There is a simple (Angular 4) route guard, which waits for some data to be loaded from backend: @Injectable() export class ContractsLoadedGuard implements CanActivate { constructor(private store: Store<State>) { } waitForData(): Observable<boolean> { return this.store.select(state => state.contracts) .map(contractList => !!contractList) .filter(loaded => loaded) .take(1); } canActivate(): Observable<boolean> { return this.waitForData(); } } Routing: const routes: Routes = [ { path: 'app-list', canActivate: [ContractsLoadedGuard], component: AppListComponent }, ]; And finally there is an @ngrx

@ngrx 4 how to filter current loaded data

只愿长相守 提交于 2019-12-04 10:21:05
I am working on a new angular 4 plus @ngrx 4 project. I wish to have a searching function on the loaded data. For example, all the contacts info have been loaded in the component. The contacts list will be filtered which contact name matched with the search text. Please see screenshot As the data is existed in store and I do not wish to call web api service again. Any idea or demo code would be appreciated. You can follow this flow to search what you need on already fetched content: Use something like '(input)'='searchInputChange$.next(search)' in your input. So, each time the user changes the

@ngrx/store subscription to part of a store and avoid detecting changes to other parts

爱⌒轻易说出口 提交于 2019-12-04 08:37:48
Background One of the reducers in my store deals with things that are selected within the app. The interface for this store (let's called it app ) could look like this: interface IApp { selectedProject: IProject; selectedPart: IPart; } So I want to have a subscription that performs an action only when a different 'project' is selected. I wrote something like this: this.store.select('app') .map((store: IApp) => store.selectedProject) .subscribe((project: IProject) => { // Stuff happens here }); This function does it stuff when selectedProject changes. However, it also does it stuff when

Understanding the purpose of the ngrx router-store project as compared to only using the angular 2 router

我的梦境 提交于 2019-12-04 08:12:58
问题 I am in reference to the router-store ngrx project (https://github.com/ngrx/router-store). I am not clear how to use this project... For instance let's take the following sample from the project documentation: store.dispatch(go(['/path', { routeParam: 1 }], { query: 'string' })); Is this meant to be use as a replacement to the angular 2 router: router.navigate(['/path... ? ...or should I use the ngrx router-store only in certain circumstances? (if so which ones?) Also what happens to the ngrx

Angular & NGRX prevent selector from emitting identical value on state change when values are equal

邮差的信 提交于 2019-12-04 08:08:19
I'm searching for a solution to make my selector only emit a new value when the it has changed compared to the last emitted value, and not only the reference to the store is changed. I have the following state in my store: { items: [], loading: false, selectedItemId: 1 } And I have the following selector: export const getSelectedItem = createSelector(getItemsState, (state) => { return state.selectedItemId === null ? null : state.items.find(item => item.id === state.selectedItemId) } ); When I do a subscribe on this selector this fires each time when for example the loading flag in the store