-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.js
65 lines (59 loc) · 2.04 KB
/
View.js
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
import icons from 'url:../../img/icons.svg';
export default class View {
// we won't create any instance of this view
// we'll only use it as a parent class of the other child views
_data;
render(data) {
if (!data || (Array.isArray(data) && data.length === 0))
return this.renderError();
//this is part of the public API
this._data = data;
const markup = this._generateMarkup();// every view that renders something to the UI needs this method
this._clear();
this._parentElement.insertAdjacentHTML('afterbegin', markup);
}
// the two properties and the render method are something that all the views will have in common
_clear() {
// this method will be available for all views with #parentElement
this._parentElement.innerHTML = ''; //get rid of existing content
}
renderSpinner() {
// this will be a public method that the controller can call as it starts fetching data
const markup = `
<div class="spinner">
<svg>
<use href="${icons}#icon-loader"></use>
</svg>
</div>
`;
this._clear();
this._parentElement.insertAdjacentHTML('afterbegin', markup);
}
renderError(message = this._errorMessage) {
const markup = `
<div class="error">
<div>
<svg>
<use href="${icons}#icon-alert-triangle"></use>
</svg>
</div>
<p>${message}</p>
</div>`;
this._clear();
this._parentElement.insertAdjacentHTML('afterbegin', markup);
}
// implement a method for success messages; we don't need it yet
renderMessage(message = this._message) {
const markup = `
<div class="message">
<div>
<svg>
<use href="${icons}#icon-smile"></use>
</svg>
</div>
<p>${message}</p>
</div>`;
this._clear();
this._parentElement.insertAdjacentHTML('afterbegin', markup);
}
}