-
Notifications
You must be signed in to change notification settings - Fork 158
/
8.1-할-일-앱-만들기-1-1.html
183 lines (161 loc) · 5.82 KB
/
8.1-할-일-앱-만들기-1-1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="jquery.js"></script>
<script src="partial.js"></script>
</head>
<body>
## 8.1 할 일 생성하기
##### [코드 8-1]
```html
<div id="todos">
<input type="text" class="create">
<ul class="list">
</ul>
</div>
```
##### [코드 8-2] 할 일 생성 및 담기
<script>//```javascript
$('body')
.on('keyup', '.create', _.pipe(
_.v('target'), // 첫 번째 인자로 e가 들어오고 e.target이 됨
_.tap( // _.tap으로 감싸면 내부 실행 후 위에서 받은 input을 아래 _.set('value'.. 에게 전달
function(input) {
return { body: input.value }; // todo key/value
},
_.t('todo', '\
li.todo_item\
.body {{todo.body}}'), // <li class="todo_item"><div class="body">내용</div></li>
$, // 위에서 생성된 문자열을 $("<li class="to"..</li>")
_('appendTo', '#todos .list')), // function(li) { li.appendTo('#todos .list'); }
_.set('value', '') // 위에서 내려온 input을 받아 input.value = '';
));
</script>```
---- 윗 부분을 취소하는 코드입니다. ----
<script>$('body').off();</script>
---- 윗 부분을 취소하는 코드입니다. ----
##### [코드 8-3] _.if 로 엔터키만 허용
<script>//```javascript
function enter_n_has_val(e) {
return e.keyCode == 13 && e.target.value;
}
$('body') // 이 부분을 통과해야 _.pipe가 리턴한 함수 실행
.on('keyup', '.create', _.if(enter_n_has_val, _.pipe(
_.v('target'),
_.tap(
function(input) {
return { body: input.value };
},
_.t('todo', '\
li.todo_item\
.body {{todo.body}}'),
$,
_('appendTo', '#todos .list')),
_.set('value', '')
)));
</script>```
## 8.1.2 체크 박스와 삭제 버튼
---- 윗 부분을 취소하는 코드입니다. ----
<script>$('body').off();</script>
---- 윗 부분을 취소하는 코드입니다. ----
##### [코드 8-4]
<script>//```javascript
function enter_n_has_val(e) {
return e.keyCode == 13 && e.target.value;
}
$('body')
.on('keyup', '.create', _.if(enter_n_has_val, _.pipe(
_.v('target'),
_.tap(
function(input) {
return { body: input.value };
},
_.t('todo', '\
li.todo_item\
input.toggle[type="checkbox"]\
.body {{todo.body}}\
button[type="button"].remove remove'), // 체크 박스와 remove 버튼 추가
$,
_('appendTo', '#todos .list')),
_.set('value', '')
)))
.on('click', '.remove', function(e) {
$(e.target).closest('.todo_item').remove(); // 삭제
});
</script>```
## 8.1.3 앱의 상태 관리를 위한 데이터 추가
---- 윗 부분을 취소하는 코드입니다. ----
<script>$('body').off();</script>
---- 윗 부분을 취소하는 코드입니다. ----
##### [코드 8-5]
<script>//```javascript
/* 범용적 코드 */
function enter_n_has_val(e) {
return e.keyCode == 13 && e.target.value;
}
/* 앱 코드 */
_.go({
list: [], // 할 일 목록을 담을 배열
}, function(app) { // 익명 함수를 _.go로 즉시 실행하여 지역화
$('body')
.on('keyup', '.create', _.if(enter_n_has_val, _.pipe(
_.v('target'),
_.tap(
function(id) { // 클로저를 이용해 고유아이디 생성 ++id
return function(input) { // 리턴 되어 파이프라인에서 사용될 함수
return app.list[app.list.length] = { // 배열에 추가하면서 동시에 todo key/value 아래로 리턴
id: ++id, body: input.value, completed: false // id, completed 추가
};
}
}(0), // 즉시 실행 함수
_.t('todo', '\
li.todo_item[todo_id="{{todo.id}}" completed="{{todo.completed}}"]\
input.toggle[type="checkbox" {{todo.completed ? "checked": ""}}]\
.body {{todo.body}}\
button[type="button"].remove remove'),
// li의 attribute로 todo_id와 completed 추가
// css에 아래 코드 추가
// .todo_item[completed="true"] .body { text-decoration: line-through; }
// completed에 따라 체크 박스의 checked 여부 결정하도록 추가
$,
_('appendTo', '#todos .list')),
_.set('value', ''),
function() {
console.log(app.list); // 할 일을 생성할 때마다 데이터에 쌓이는지 확인해보기
// [{id: 1 ...}]
// [{id: 1 ...}, {id: 2 ...}]
// [{id: 1 ...}, {id: 2 ...}, {id: 3 ...}]
}
)));
function e_to_todo_item(e) {
return $(e.target).closest('.todo_item');
}
function todo_item_to_todo(todo_item) {
return _.find_where(app.list, { id: todo_item.attr('todo_id') });
}
$('body')
.on('click', '.remove', _.pipe(
e_to_todo_item, // 아래 (1)에도 동일 코드가 필요해 함수로 만들고 중복 제거
_('remove'), // _.partial로 위에서 내려올 element의 메서드 remove를 실행하는 함수 생성,
todo_item_to_todo, // 아래 (2)에도 동일 코드가 필요해 함수로 만들고 중복 제거
_(_.remove2, app, 'list'), // app.list에서 위에서 내려온 todo를 제거
function() {
console.log(app.list); // 잘 지워졌는지 확인해보기
}
))
.on('change', '.toggle', _.pipe(
function(e) {
return e_to_todo_item(e).attr('completed', e.target.checked); // (1) 재사용
},
todo_item_to_todo, // (2) 재사용
_.set('completed', _.not), // 위에서 내려오는 todo에 todo.completed = !todo.completed 실행,
function() {
console.log(app.list); // 상태가 잘 변경되었는지 확인해보기
}
));
});
</script>```
</body>
</html>