|
1 | 1 | import { destinations } from '../mocks/destinations.js'; |
2 | 2 | import { offers } from '../mocks/offers.js'; |
3 | 3 | import { points } from '../mocks/points.js'; |
| 4 | +import Observable from '../framework/observable.js'; |
4 | 5 |
|
5 | | -export default class PointModel { |
| 6 | +export default class PointModel extends Observable { |
6 | 7 | points = points; |
7 | 8 | offers = offers; |
8 | 9 | destinations = destinations; |
@@ -36,4 +37,44 @@ export default class PointModel { |
36 | 37 |
|
37 | 38 | return allDestinations.find((item) => item.id === id); |
38 | 39 | } |
| 40 | + |
| 41 | + updatePoint(updateType, update) { |
| 42 | + const index = this.points.findIndex((point) => point.id === update.id); |
| 43 | + |
| 44 | + if (index === -1) { |
| 45 | + throw new Error('Can\'t update unexisting task'); |
| 46 | + } |
| 47 | + |
| 48 | + this.points = [ |
| 49 | + ...this.points.slice(0, index), |
| 50 | + update, |
| 51 | + ...this.points.slice(index + 1), |
| 52 | + ]; |
| 53 | + |
| 54 | + this._notify(updateType, update); |
| 55 | + } |
| 56 | + |
| 57 | + addPoint(updateType, update) { |
| 58 | + this.points = [ |
| 59 | + update, |
| 60 | + ...this.points, |
| 61 | + ]; |
| 62 | + |
| 63 | + this._notify(updateType, update); |
| 64 | + } |
| 65 | + |
| 66 | + deletePoint(updateType, update) { |
| 67 | + const index = this.points.findIndex((point) => point.id === update.id); |
| 68 | + |
| 69 | + if (index === -1) { |
| 70 | + throw new Error('Can\'t delete unexisting task'); |
| 71 | + } |
| 72 | + |
| 73 | + this.points = [ |
| 74 | + ...this.points.slice(0, index), |
| 75 | + ...this.points.slice(index + 1), |
| 76 | + ]; |
| 77 | + |
| 78 | + this._notify(updateType); |
| 79 | + } |
39 | 80 | } |
0 commit comments