Last active
April 21, 2021 23:59
-
-
Save ScriptBytes/93273fe6652bde6ae58d3d3f36797b1a to your computer and use it in GitHub Desktop.
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 { HttpClient } from '@angular/common/http'; | |
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject} from 'rxjs'; | |
import { StoreSettings } from './models/store-settings.model'; | |
@Injectable({ providedIn: 'root' }) | |
export abstract class StoreService<T> { | |
protected itemsSubject = new BehaviorSubject<T[]>([]); | |
items$ = this.itemsSubject.asObservable(); | |
protected get items(): T[] { | |
return this.itemsSubject.getValue(); | |
} | |
protected set items(val: T[]) { | |
this.itemsSubject.next([...val]); | |
} | |
constructor( | |
protected http: HttpClient, | |
protected settings: StoreSettings | |
) {} | |
load() { | |
const url = this.settings.url; | |
this.http | |
.get<T[]>(url) | |
.subscribe((res) => { | |
this.items = res; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment