-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorepattern.txt
111 lines (84 loc) · 2.75 KB
/
storepattern.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
https://github.com/ale-fuentes-ar/rxjs-storepattern
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { Product } from '../models/product';
@Injectable({
providedIn: 'root'
})
export class StoreService {
// this list where I save the product selected by the user
private myList: Product[] = [];
// this is myBag where all selected product flow
private myBag = new BehaviorSubject<Product[]>([]);
// this is my observable object, where all components can see the products flow
myBag$ = this.myBag.asObservable();
constructor() { }
addProduct(product: Product){
// add a new product in my bag
this.myList.push(product);
// emmit and update my behaviorSubject object the 'myBag'.
this.myBag.next(this.myList);
}
}
*******************
import { Component, OnInit } from '@angular/core';
import { Product } from 'src/app/models/product';
import { ProductsService } from 'src/app/services/products.service';
// importing dependenci of Store service
import { StoreService } from 'src/app/services/store.service';
@Component({
selector: 'app-products',
templateUrl: './products.component.html',
styleUrls: ['./products.component.css']
})
export class ProductsComponent implements OnInit {
private products: Product[] = [];
constructor(
private productService: ProductsService,
// injecting Store service in my component
private storeService: StoreService,
) { }
ngOnInit() {
this.getProducts();
}
getProducts() {
this.productService.getProducts().subscribe(data => {
this.products = data;
});
}
addProductToBag(product: Product) {
// add product in my Observable
this.storeService.addProduct(product);
}
}
*******************
<div class="card-body">
<h5 class="card-title">{{ product.title }}</h5>
<p class="card-text">{{ product.description }}</p>
<!-- this click call method that add new product in Store service -->
<button (click)="addProductToBag(product)" class="btn btn-primary">Add to Bag</button>
</div>
*******************
import { Component, OnInit } from '@angular/core';
// importing dependenci of Store service
import { StoreService } from 'src/app/services/store.service';
@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {
// See subscribe form Store service
myBag$ = this.storeService.myBag$;
constructor(
// injecting Store service in my component
private storeService: StoreService,
) { }
ngOnInit() {
}
}
*******************
<div class="item-product" *ngFor="let product of (myBag$ | async)">
<img [src]="product.images[0]" alt="">
<p>{{ product.title }}</p>
</div>