I am trying to learn @ngrx/data by creating simple TODO app
I created simple ToDoDataService to override default HTTP
@Injectable()
export class TodosDataService extends DefaultDataService<Todo> {
constructor(httpClient: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
super('Todo', httpClient, httpUrlGenerator);
}
getAll(): Observable<Array<Todo>> {
console.log('asfsfsfsf')
return this.http.get('https://jsonplaceholder.typicode.com/todos')
.pipe(
map((res: any) => res)
);
}
getWithQuery(query): Observable<Array<any>> {
console.log('asfsfsfsf', query);
return this.http.get(`https://jsonplaceholder.typicode.com/todos?${query}`)
.pipe(
map((res: any) => res)
);
}
}
In the route-resolver i am doing like this
@Injectable()
export class TodosResolver implements Resolve<boolean> {
constructor(private todoService: TodoEntityService) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.todoService.loaded$
.pipe(
tap(loaded => {
if (!loaded) {
this.todoService.getAll();
}
}),
filter(loaded => !!loaded),
first()
);
}
}
This is working fine and store/entities is updated
But in my component I am doing like this
export class HomeComponent implements OnInit {
loading$: Observable<boolean>;
todos$: Observable<Todo[]>;
constructor(
private todoService: TodoEntityService
) { }
ngOnInit() {
console.log('dsdf');
this.todos$ = this.todoService.entities$
.pipe(
tap(todos => {
console.log(todos);
this.loadMore();
console.log(todos);
}),
map((todos: any) => todos),
first()
);
}
loadMore() {
this.todoService.getWithQuery('_start=20&_limit=5');
}
}
Here API is calling but entities is still showing old data. Not sure what i am doing wrong,
Please help
"[HomeComponent ONINIT] ONINIT"
then create effect that callsloadMore()
. Otherwise you'll end up with just a QUERY_ALL that is hard to trace, especially of you call it in multiple places. Similarly with the resolver"[TodosResolver RESOLVE] RESOLVE TODOS"
– Andrew Allen