diff --git a/index.js b/index.js index a33aea5..41dd3b3 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,7 @@ const addBook = () => { bookView.render(model.state.books); }; -const removeBook = (e) => { - const id = bookView.getRemovedId(e); +const removeBook = (id) => { model.removeBook(id); bookView.render(model.state.books); }; diff --git a/modules/views/bookView.js b/modules/views/bookView.js index 6ab629b..c9c3a5f 100644 --- a/modules/views/bookView.js +++ b/modules/views/bookView.js @@ -1,8 +1,6 @@ class BookView { #parentEl = document.querySelector('.books-container'); - #targetId; - render(books) { this.#parentEl.innerHTML = ''; if (!books) return; @@ -17,15 +15,14 @@ class BookView { }); } - getRemovedId(e) { - if (e.target.classList.contains('remove')) { - this.#targetId = +e.target.dataset.bookid; - } - return this.#targetId; - } - addHandlerRemover(handler) { - this.#parentEl.addEventListener('click', handler); + this.#parentEl.addEventListener('click', (e) => { + const btn = e.target.closest('.remove'); + if (!btn) return; + const bookId = +btn.dataset.bookid; + + handler(bookId); + }); } }