Skip to content

Instantly share code, notes, and snippets.

@JeffryGonzalez
Created February 1, 2021 21:13
Show Gist options
  • Save JeffryGonzalez/292f1dd25f8ea629833835599cc04aa4 to your computer and use it in GitHub Desktop.
Save JeffryGonzalez/292f1dd25f8ea629833835599cc04aa4 to your computer and use it in GitHub Desktop.
shopping
import { ShoppingListItem } from '../models';
import { BehaviorSubject, Observable } from 'rxjs';
export class ShoppingDataService {
private fakeData: ShoppingListItem[] = [
{ id: '1', description: 'Ice Cream Cones', purchased: false },
{ id: '2', description: 'Shoe Polish', purchased: true }
];
private maxId = 3;
private subject = new BehaviorSubject<ShoppingListItem[]>(this.fakeData);
getObservable(): Observable<ShoppingListItem[]> {
return this.subject.asObservable();
}
addItem(description: string): void {
this.fakeData.push({ id: (this.maxId++).toString(), description, purchased: false });
// this would be some async code that goes to the api.
this.subject.next(this.fakeData);
}
markItemAsPurchased(item: ShoppingListItem): void {
const storedItem = this.fakeData.filter(i => i.id === item.id)[0].purchased = true;
// this would be some async code that goes to the api.
this.subject.next(this.fakeData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment