//TaskList.js
class TaskList extends EventEmitter {
constructor (initialTask = []) {
super();
this.tasks = [...initialTask];
}
getTasks () {
return this.tasks;
}
addTask (task) {
this.tasks.push(task);
this.trigger('addTask', task);
this.triggerTask();
}
triggerTask () {
this.trigger('tasks', this.getTasks());
}
}
// index.js
(function (d) {
const input = d.querySelector('input');
const button = d.querySelector('button');
// Default tasks
const initialTasks = ['Task 1', 'Task 2'];
const taskList = new TaskList(initialTasks);
taskList.on('tasks', task => console.log(task));
taskList.on('addTask', task => console.log('[EventEmitter]: Add New Task ' + task))
button.addEventListener('click', (_) => validInput(input, value => taskList.addTask(value)));
// Field validation
let validInput = (elem, fn) => {
const trimmedValue = elem.value.trim();
if (trimmedValue.length === 0) return;
fn(trimmedValue);
elem.value = '';
}
taskList.triggerTask(); // task insulation
})(document)
'use strict';
(function (d) {
const input = d.querySelector('input');
const button = d.querySelector('button');
const initialTask = ['Tesk 1', 'Task 2'];
const tasks = [...initialTask];
const events = new EventEmitter;
events.on('tasks', task => {
console.log(task);
});
events.on('addTask', (task, val) => {
console.log('[EventEmitter] Add Task ' + val);
events.trigger('tasks', task);
});
events.trigger('tasks', tasks);
button.addEventListener('click', () => {
const val = input.value;
if (val.length === 0) {
return;
}
tasks.push(val);
events.trigger('addTask', tasks, val);
input.value = '';
});
})(document);
Какие данные нужно вытягивать, чтобы потом сверять?
А тип я убрал уже :)