Commit d2e32429 authored by Milan Iliev's avatar Milan Iliev

Task model + reactive editing

parent 170ec368
let tasks = [ class Task extends EventTarget {
{done: true, description: "Do Laundry"}, constructor(data){
{done: false, description: "Do Homework"}, super()
{done: false, description: "Do Chores"}, this.data = data
}
get done(){
return this.data.done
}
set done(new_value){
this.data.done = new_value
this.dispatchEvent(new CustomEvent("change"))
}
get description(){
return this.data.description
}
set description(new_value){
this.data.description = new_value
this.dispatchEvent(new CustomEvent("change"))
}
}
let tasks = [
new Task({done: true, description: "Do Laundry" }),
new Task({done: false, description: "Do Homework"}),
new Task({done: false, description: "Do Chores" }),
] ]
class TaskView { class TaskView {
...@@ -15,11 +39,23 @@ class TaskView { ...@@ -15,11 +39,23 @@ class TaskView {
this.task = task this.task = task
this.element = document.createElement("task-summary") this.element = document.createElement("task-summary")
this.element.innerHTML = this.constructor.template this.element.innerHTML = this.constructor.template
this.fields = {
done: this.element.querySelector("[name=done]"),
}
this.update() this.update()
this.task.addEventListener("change", () => this.update())
this.fields.done.addEventListener("change", () => {
this.task.done = this.fields.done.checked
})
} }
update() { update() {
this.element.querySelector("[name=done]").checked = this.task.done this.fields.done.checked = this.task.done
this.element.querySelector("task-description").innerText = this.task.description this.element.querySelector("task-description").innerText = this.task.description
} }
} }
...@@ -28,11 +64,33 @@ class DetailTaskView { ...@@ -28,11 +64,33 @@ class DetailTaskView {
constructor(element){ constructor(element){
this.element = element this.element = element
this.fields = {
done: this.element.querySelector("[name=done]"),
description: this.element.querySelector("[name=description]")
}
this.fields.description.addEventListener("input", () => {
this.task.description = this.fields.description.value
})
this.fields.done.addEventListener("change", () => {
this.task.done = this.fields.done.checked
})
} }
update(task) { get task(){
this.element.querySelector("[name=done]").checked = task.done return this._task
this.element.querySelector("[name=description]").value = task.description }
set task(new_task){
this._task = new_task
this._task.addEventListener("change", () => this.update())
this.update()
}
update() {
this.fields.done.checked = this.task.done
this.fields.description.value = this.task.description
} }
} }
...@@ -43,7 +101,7 @@ let list = document.querySelector("task-list") ...@@ -43,7 +101,7 @@ let list = document.querySelector("task-list")
for (let task of tasks){ for (let task of tasks){
let view = new TaskView(task) let view = new TaskView(task)
view.element.addEventListener("click", () => detail_view.update(task) ) view.element.addEventListener("click", () => detail_view.task = task )
list.appendChild(view.element) list.appendChild(view.element)
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment