Skip to content

Instantly share code, notes, and snippets.

@rosd89
Last active August 23, 2017 11:22
Show Gist options
  • Save rosd89/207c38a7a308432db4216f4b0a8f7c36 to your computer and use it in GitHub Desktop.
Save rosd89/207c38a7a308432db4216f4b0a8f7c36 to your computer and use it in GitHub Desktop.
Vue·tiful Korea 170823 컴포넌트야 놀자
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>todo</title>
</head>
<body>
<div id="root"></div>
</body>
<script src="https://unpkg.com/vue"></script>
<script type="application/javascript">
Vue.component('todo-content', {
template: `
<div class="todo-content">
<h1>Todo List</h1>
<slot></slot>
</div>`
});
Vue.component('todo-list', {
data() {
return {
inputItem: '',
items: [
{job: 'A', isComplete: false},
{job: 'B', isComplete: false},
{job: 'C', isComplete: false}
]
}
},
methods: {
completedItem(job, isComplete) {
const item = this.items.find(v => v.job === job);
item.isComplete = isComplete;
},
deleteItem(job) {
this.items = this.items.filter(v => job !== v.job);
},
appendTodoItem() {
if (!this.inputItem) {
window.alert('할일을 입력해주세요.');
return false;
}
this.items.push({job: this.inputItem, isComplete: false});
this.inputItem = '';
}
},
template: `
<div>
<input class="input-text" type="text" v-model="inputItem"/>
<button class="btn" @click="appendTodoItem">추가</button>
<ul>
<todo-item
v-for="item in items"
:key="item.job"
:job="item.job"
:isComplete="item.isComplete"
v-on:completedItem="completedItem"
v-on:deleteItem="deleteItem"></todo-item>
</ul>
</div>`
});
Vue.component('todo-item', {
props: ['job', 'isComplete'],
data() {
return {
_isComplete: false
}
},
mounted() {
this._isComplete = this.isComplete;
},
computed: {
printJob() {
return this.isComplete ?
`<span class="completed">${this.job}</span>`:
`${this.job}`
}
},
methods: {
completeJob() {
this._isComplete = !this._isComplete;
this.$emit('completedItem', this.job, this._isComplete);
},
deleteJob() {
this.$emit('deleteItem', this.job);
}
},
template: `
<li>
<span v-html="printJob"></span>
<button v-if="isComplete" class="btn" @click="deleteJob">삭제</button>
<button v-else class="btn" @click="completeJob">완료</button>
</li>`
});
new Vue({
el: '#root',
template: `
<todo-content>
<todo-list></todo-list>
</todo-content>`,
});
</script>
<style>
.todo-content {
width: 720px;
background: #dddddd;
margin: 0 auto;
padding: 1px 5px 10px 5px;
}
.btn {
clear: both;
float: right;
}
.input-text {
width: 200px;
margin-left: 20px;
}
.completed {
text-decoration: line-through;
}
</style>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment