Last active
March 16, 2019 09:28
-
-
Save AmirTugi/8a63ff9371bb283d24e70930ddcf761f to your computer and use it in GitHub Desktop.
Independent Search Component Akita Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { categoriesMock } from './categories-mock'; | |
| import { of } from 'rxjs'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class CategoryApiService { | |
| search() { | |
| return of(categoriesMock) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { CategoryModel } from './model'; | |
| export const categoriesMock: CategoryModel[] = [ | |
| {id: 1, name: 'Hobbies'}, | |
| {id: 2, name: 'Garening'}, | |
| {id: 3, name: 'Home Appliances'}, | |
| {id: 4, name: 'Consumer Electronics'}, | |
| {id: 5, name: 'Clothing'}, | |
| {id: 6, name: 'Gadgets'}, | |
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class CategoryModel { | |
| id: number; | |
| name: string; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { QueryEntity } from '@datorama/akita'; | |
| import { CategoryStore, CategoryState } from './store'; | |
| import { CategoryModel } from './model'; | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class CategoryQuery extends QueryEntity<CategoryState, CategoryModel> { | |
| constructor(protected store: CategoryStore) { | |
| super(store); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { ID } from '@datorama/akita'; | |
| import { CategoryStore } from './store'; | |
| import { CategoryModel } from './model'; | |
| import {tap} from 'rxjs/operators'; | |
| import { CategoryApiService } from './api.service'; | |
| @Injectable({ providedIn: 'root' }) | |
| export class CategoryStoreService { | |
| constructor(private categoryStore: CategoryStore, | |
| private categoryApiService: CategoryApiService) { | |
| } | |
| search() { | |
| return this.categoryApiService.search() | |
| .pipe( | |
| tap(categories => this.categoryStore.set(categories)) | |
| ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable } from '@angular/core'; | |
| import { EntityState, EntityStore, StoreConfig } from '@datorama/akita'; | |
| import { CategoryModel } from './model'; | |
| export interface CategoryState extends EntityState<CategoryModel> {} | |
| @Injectable({ providedIn: 'root' }) | |
| @StoreConfig({ name: 'category' }) | |
| export class CategoryStore extends EntityStore<CategoryState, CategoryModel> {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment