View
export default class SearchResultView extends View {
constructor() {
super(qs("#search-result-view"));
this.template = new Template();
}
show(data = []) {
if (data.length > 0) {
this.element.innerHTML = this.template.getList(data);
} else {
this.element.innerHTML = this. template.getEmptyMessage();
}
super.show();
}
}
์ด๋ฒ์๋ ๊ฒ์ ๊ฒฐ๊ณผ ๊ตฌํ์ ์ํด ์ ์ ํ์ด์ง์์ search-result-view๋ฅผ ์์ฑํ๊ณ query selector๋ก ํธ์ถํ๋ค.
๊ทธ๋ผ SearchResultView์ element ๋ณ์๋ ๊ฒ์ ๊ฒฐ๊ณผ ํ๊ทธ์ ์ ๋ณด๋ฅผ ๊ฐ๊ฒ ๋๋ค.
Template์ class๋ก ์ด๋ป๊ฒ ๋ ๋๋ง ํ ๊ฒ์ธ์ง์ ๋ํด ๋ช ์ํด๋ ์ฝ๋์ด๋ค.
์ฆ, Controller ์์์ ์์ ์ ๋ง์น๊ณ View์ show ๋ฉ์๋๋ก ๋ ๋๋ง์ ํ ๊ฒ์์ ์ ์ ์๋ค.
Controller
export default class Controller {
constructor(store, {searchFormView, searchResultView}) {
this.store = store;
this.searchFormView = searchFormView;
this.searchResultView = searchResultView;
}
์ถ๊ฐ๋ก SearchResultView์ ๋ํ ์์กด์ฑ๋ ์ถ๊ฐํด์ค๋ค.
์์ ๊ฒ์ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ์์ ์์ฑ๋, search()์ reset()์ ์๋น์ค ๋ก์ง์ ์์ฑํ๋ฉด ๋๋ค.
Model
export default class Store {
constructor(storage) {
if (!storage) throw "no storage";
this.storage = storage;
this.searchResult = [];
this.searchKeyword = "";
}
}
searchKeyword์ searchResult๋ฅผ ๋ด์ ๋ณ์๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ Business Logic์ ์์ฑํด์ค๋ค.
Quest
Q. reset ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
// controller
reset() {
this.store.reset();
this.render();
}
// model
reset() {
this.searchKeyword = "";
}
๊ฐ์ฌ๋์ Controller reset() ๋ฉ์๋์ this.store.searchKeyword = ""๋ฅผ ์ถ๊ฐํ๋ค.
๋๋ keyword๊ฐ Store ๋ผ๋ Model์ ์ข ์๋ ๋ณ์์ด๋ฏ๋ก Store ๊ฐ์ฒด์์ ์ง์ ์ ๊ทผํ๋๋ก ์ธ์คํด์ค ๋ฉ์๋๋ก ๋ง๋ค์๋ค.
์ถ๊ฐ๋ก, ๊ฐ์ฌ๋์ searchResult ๋ํ ๋น ๋ฐฐ์ด๋ก ์ด๊ธฐํํ์๋๋ฐ, ๋๋ render() ์์ ์์ searchResult๊ฐ update ๋๋ฏ๋ก ๊ตณ์ด ์์ฑํ์ง๋ ์์๋ค.
Result
โป ์ด์ Post์์์ ๊ถ๊ธํ๋ ์ ํด๊ฒฐ
Controller ์์ Service ๋ก์ง์ ์์ฑํ๋ค.
'JavaScript > Vanila' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Vanila js] ํค์๋ ๊ฒ์ ๊ตฌํํ๊ธฐ (0) | 2024.08.02 |
---|---|
[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 |