Branch - ready/search-form-1
์์ ํฌ์คํ ์ ์์ ์ด ์ ๋๋ก ๋์๋ค๋ฉด main ๋ธ๋์น์์
1. git checkout -b ready/search-form-1
2. ์ฝ๋ ์์ฑ
3. git add .
4. git commit -m "feat: ๋ด์ฉ"
5. git push origin ready/search-form-1
์ดํ PR ๋ ๋ฆฌ๊ณ Rebase Merge ํ๊ธฐ.
git bash์์ ์ฝ๋๋ก ํ ์๋ ์์ง๋ง, ํ์ ์ค ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ํ๋ ๊ณผ์ ์ด ์์.
๊ทธ๋์ ์ง์ github ์ฌ์ดํธ์์ Mergeํ๋ ๊ฒ์ ์ต์ํด์ง๊ธฐ
์์ผ๋ก ๋ชจ๋ ์์ ์ ์ ๊ณผ์ ์ ๋ฐ๋ณตํด์ branch๋ฅผ ์ ๋ฐ์ดํธ ํ ์์
// index.html
<script type="module" src="./js/main.js"></script>
// main.js
document.addEventListener("DOMContentLoaded", main);
function main() {}
- ์ ์ ํ์ด์ง์ module๋ก main.js๊ฐ ์ถ๊ฐ๋ ๊ฒ์ ์ ์ ์๋ค.
์ฆ, main์ ์ฝ๋๋ ๋งจ ์ฒ์ index.html์ DOM์์ ๋ถ๋ฌ์ฌ ๋, main.js์ ์์ฑ๋ ์ฝ๋๋ค์ ๋ชจ๋ ๋ถ๋ฌ์จ๋ค. - main.js ์์ dom์ด load๋ ๋, main method๋ฅผ eventListener๋ก ์ถ๊ฐํ๋ค.
- closer ๊ฐ๋ ์ผ๋ก main method๋ ํ๋์ instance๊ฐ ๋๊ณ main method์์ ์์ฑ๋ ์ฝ๋์ ๊ณ์ํด์ ์ ๊ทผํ ์ ์๋ค.
์ฆ, ๋ญ ์์ฑํ ์ฝ๋๊ฐ ์์ผ๋ฉด main method์์ ์ฒ๋ฆฌํ๋ฉด ๋๋ค.
๊ฐ์ฌ๋์ ์ด๊ฒ์ MVC ํจํด์ ํ์ฉํด์ ์ฒ๋ฆฌํ๋ค.
ํด๋ก์ ์ ์์ธํ ์ค๋ช : https://developer.mozilla.org/ko/docs/Web/JavaScript/Closures
ํด๋ก์ - JavaScript | MDN
ํด๋ก์ ๋ ์ฃผ๋ณ ์ํ(์ดํ์ ํ๊ฒฝ)์ ๋ํ ์ฐธ์กฐ์ ํจ๊ป ๋ฌถ์ธ(ํฌํจ๋) ํจ์์ ์กฐํฉ์ ๋๋ค. ์ฆ, ํด๋ก์ ๋ ๋ด๋ถ ํจ์์์ ์ธ๋ถ ํจ์์ ๋ฒ์์ ๋ํ ์ ๊ทผ์ ์ ๊ณตํฉ๋๋ค. JavaScript์์ ํด๋ก์ ๋ ํจ์ ์
developer.mozilla.org
export default class SearchFormView extends View {
constructor() {
super(qs("#search-form-view"));
this.inputElement = qs("[type=text]", this.element);
this.resetElement = qs("[type=reset]", this.element);
this.showResetButton(false);
this.bindEvents()
}
}
View๋ฅผ ํ์ฅ๋ฐ๋ ๊ฒ์ View๋ฅผ ์ดํด๋ณด์
1. ์ ์ ํ์ด์ง์ ์์ฑ๋ ๊ฒ์ ํผ ํ๊ทธ๋ฅผ ์ ํํ๋ค.
2. ํด๋น ํ๊ทธ์ ์ํด์๋ ๋ ์ ์ค type ์์ฑ์ด text์ reset์ธ ๊ฒ์ ๊ฐ์ ธ์จ๋ค.
3. ๊ทธ๋ฆฌ๊ณ ํด๋น ํ๊ทธ๋ค์ ๋ํ ๊ธฐ๋ฅ๋ค์ ์์ฑํ๋ค. -> ๊ธฐ๋ฅ๋ณ๋ก ์ ๋ถ๋ฆฌ๋ ์ฝ๋
bindEvents() {
on(this.inputElement, "keyup", () => this.handleKeyup());
on(this.element, "submit", event => this.handleSubmit(event));
on(this.resetElement, "click", () => this.handleReset());
}
handleSubmit(event) {
event.preventDefault();
const {value} = this.inputElement;
this.emit("@submit", {value});
}
์ด๋ฐ์์ผ๋ก ๊ฐ ๋ฉ์๋์ ๋ํ event๋ฅผ helper.js์์ utility๋ก ๊ตฌํํ on ๋ฉ์๋๋ก ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํ๊ณ ํธ๋ค๋ฌ ์์ ์ ํ๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก HTML์๋ ํน์ ์ด๋ฒคํธ์ ๋ํ ์์ ์ด ๋์ด์๋ค. ์ถ๊ฐ์ ์ธ ์์ ์ด๋ ๊ธฐ์กด ์์ ์ ์์จ ๊ฒฝ์ฐ, ํธ๋ค๋ฌ๋ฅผ ์ฌ์ฉํด์ผํ๋ค.
emit ๋ํ, helper.js์์ ๊ตฌํ๋ utility์ด๋ค.
์๋ก์ด ์ด๋ฒคํธ๋ฅผ ๋ง๋ค์ด์ ์ ๋ฌํ๋ค.
export default class Controller {
constructor(store, {searchFormView}) {
this.store = store;
this.searchFormView = searchFormView;
this.subscribeViewEvents();
}
subscribeViewEvents() {
this.searchFormView
.on('@submit', (event) => this.search(event.detail.value))
.on('@reset', () => this.reset());
}
}
View์์ ์ ๋ฌ๋ ์์ ์ Controller์์ Catchํ์ฌ ์์ธ ์์ ์ ํ๋ค.
๊ถ๊ธํ ์
1. Controller์์ Business Logic์ ์์ฑํ ๊น?
2. Model์์ Business Logic์ ์์ฑํ๊ณ Controller์์ store ๊ฐ์ฒด๋ฅผ ์ด์ฉํด ์๋น์ค ์์ ์ ํ ๊น?
3. ๊ทธ ๋ค์ ์์ ์ด ๋๋ ๋ค ๋ฐํํ ๋๋ View์๊ฒ ๋ค์ Event๋ฅผ ์ฃผ๋๊ฑธ๊น?
4. Controller์์ ๋ณํ๋ ๋ ๋๋ง ์์ ์ ํ ๊น?
ํ์คํธ
๊ฒ์์ด Reset ๋ฒํผ ํด๋ฆญ ์, ๊ฒ์ ๋ด์ฉ ์ ๊ฑฐ ๋ฐ ๊ฒ์์ด ์ง์ฐ๋ ์์
// View
bindEvents() {
on(this.resetElement, "click", () => this.handleReset());
}
handleReset() {
this.emit("@reset");
this.showResetButton(false);
}
// Controller
subscribeViewEvents() {
this.searchFormView
.on('@reset', () => this.reset());
}
reset() {
console.log("reset");
}
1. input tag์ ์์ฑ์ธ reset type์ HTML์์ ์ ๊ณตํ๋ form data๋ฅผ ๋ชจ๋ ์ง์ด๋ค.
-> ๊ทธ๋์ ๋ฐ๋ก ์ง์ฐ๋ ์์ ์ ํ์ง ์์๋ ๋๋ค.
2. ์ดํ ๊ฒ์ ๋ด์ฉ์ ์ง์ฐ๊ธฐ ์ํด์ Controller์์ ์์ ์ด ๋ฐ์ํ ํ ๋๊น, ์ด๋ฒคํธ๋ฅผ ํธ๋ค๋ฌ๋ก ๋ฐ์ธ๋ฉํด๋์๋ค.
3. ๋ฐ์ธ๋ฉ๋ ํธ๋ค๋ฌ ์ด๋ฒคํธ๋ Controller์์ ์์ ์ด ์งํ๋๋ค.
4. ์์ ์ด ๋๋๋ฉด View ๋จ์์ ๋ณด์ฌ์ง Btn์ ์จ๊ธฐ๊ธฐ์ํด showResetButton ๋ฉ์๋๋ฅผ ์คํํ๋ค.
handleKeyup() {
const {value} = this.inputElement;
if (value.length <= 0) this.handleReset();
}
๊ฐ์ฌ๋ ๊ฒ์๋ด์ฉ์ ์์ฑํ๋ค ์ง์ธ ๋๋ Reset์ ํ๋๋ก ํ๋ค.
'JavaScript > Vanila' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vanila js] ํญ ๊ตฌํํ๊ธฐ (0) | 2024.08.01 |
---|---|
[Vanila js] ๊ฒ์ ๊ฒฐ๊ณผ ๊ธฐ๋ฅ ๊ตฌํ (0) | 2024.07.31 |
[Vanila.js] ์ฌ์ ์ฝ๋ ์ดํดํ๊ธฐ (0) | 2024.07.31 |
[Vanila.js] ํ๊ฒฝ์ ํ 1 (0) | 2024.07.30 |
[Vanila.js] ํ๊ฒฝ์ ํ 2 (0) | 2024.07.30 |