Skip to content

Commit f7fd7ed

Browse files
committed
feature: data and event binding
1 parent 22c4e78 commit f7fd7ed

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

todo-list/src/App.vue

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,25 @@
2222
<div class="row">
2323
<div class="column">
2424
<div class="card">
25-
<div class="row">
25+
<div class="row" v-for="(task, i) in tasks" :key="i" :class="{ 'task-done': task.done }">
2626
<div class="column column-20">
27-
<button class="button-check button-clear">
27+
<button class="button-check button-clear" @click="toggleTask(i)">
2828
&nbsp;
2929
</button>
3030
</div>
3131
<div class="column column-80">
32-
<h3>Buy Milk</h3>
32+
<h3 class="no-margin">{{ task.title }}</h3>
3333
</div>
3434
</div>
35-
<div class="row task-done">
36-
<div class="column column-20">
37-
<button class="button-check button-clear">
38-
&nbsp;
39-
</button>
40-
</div>
41-
<div class="column column-80">
42-
<h3>Buy Milk</h3>
43-
</div>
35+
<div v-if="!tasks.length">
36+
<h3 class="no-margin">No tasks, yet.</h3>
4437
</div>
4538
</div>
4639
</div>
4740
</div>
4841
<div class="row add-task">
4942
<div class="column text-center">
50-
<button class="button">
43+
<button class="button" @click="addTask()">
5144
<h3 class="no-margin">Add Task</h3>
5245
</button>
5346
</div>
@@ -61,11 +54,37 @@
6154
</template>
6255

6356
<script>
57+
/**
58+
* New task entity
59+
* @param {string} title - The title of this task
60+
* @param {boolean} title - Whether the task is done or not
61+
*/
62+
function Task(title, done) {
63+
this.title = title || 'New Task';
64+
this.done = done || false;
65+
}
66+
6467
export default {
6568
name: 'app',
6669
data () {
6770
return {
68-
message: 'Hello World!'
71+
tasks: [],
72+
}
73+
},
74+
methods: {
75+
/**
76+
* Add a new task to the array of tasks
77+
*/
78+
addTask () {
79+
const newTask = new Task();
80+
this.tasks.push(newTask);
81+
},
82+
/**
83+
* Toggle the `done` status of a task
84+
* @param {number} i - Index of the task
85+
*/
86+
toggleTask (i) {
87+
this.tasks[i].done = !this.tasks[i].done;
6988
}
7089
}
7190
}

0 commit comments

Comments
 (0)