-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies.js
57 lines (40 loc) · 1.46 KB
/
movies.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
import pubsub from './pubsub.js';
class Movies {
constructor(list = []) {
this.list = list;
this.movieDeleted = this.movieDeleted.bind(this);
this.movieAdded = this.movieAdded.bind(this);
}
render(container) {
const template = document.querySelector('#movieListTemplate');
const div = template.content.cloneNode(true);
container.appendChild(div);
const ul = document.querySelector('.movie-container ul');
ul.addEventListener('click', this.movieDeleted);
pubsub.subscribe('movieAdded', this.movieAdded);
}
movieAdded(name) {
const list = new Set(this.list);
list.add(name);
this.list = Array.from(list).sort();
pubsub.publish('moviesUpdated', this.list);
const ul = document.querySelector('.movie-container ul');
ul.innerHTML = '';
const df = document.createDocumentFragment();
this.list.forEach(movie => {
const li = document.createElement('li');
li.textContent = movie;
df.appendChild(li);
});
ul.appendChild(df);
}
movieDeleted(event) {
const item = event.target.closest('li');
const movieName = item.textContent;
this.list = this.list.filter(movie => movie !== movieName);
item.parentElement.removeChild(item);
pubsub.publish('movieDeleted', this.list);
}
}
const movies = new Movies();
export default movies;