Skip to content

Instantly share code, notes, and snippets.

@ScriptBytes
Last active April 21, 2021 23:59
Show Gist options
  • Save ScriptBytes/93273fe6652bde6ae58d3d3f36797b1a to your computer and use it in GitHub Desktop.
Save ScriptBytes/93273fe6652bde6ae58d3d3f36797b1a to your computer and use it in GitHub Desktop.
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