[Vanila js] ํค์๋ ๊ฒ์ ๊ตฌํํ๊ธฐ
Result
View
export default class KeywordListView extends View {
constructor() {
super(qs('#keyword-list-view'));
this.template = new Template();
this.bindEvents();
}
show(data = []) {
}
bindEvents() {
}
handleClick(event) {
}
}
์์ ๊ธฐ๋ฅ๋ค๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก keyword-list-view์ ์ ์ฉํ interface๋ค.
Result๋ฅผ ๋ณด๋ฉด ํค์๋๋ฅผ ํด๋ฆญํ๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋๊น ์ด๋ฒคํธ๋ฅผ ๋ฐ์ธ๋ฉํด์ฃผ๋ ๊ฒ์ด ํ์ํ๊ณ
keyword ๋ชฉ๋ก๋ค์ ๋ณด์ฌ์ฃผ์ด์ผ ํ๋ฏ๋ก show() ๋ฉ์๋๊ฐ ๊ตฌํ๋ ๊ฒ์ด๋ค.
Controller
export default class Controller {
constructor(store, {searchFormView, searchResultView, tabView, keywordListView}) {
this.store = store;
this.searchFormView = searchFormView;
this.searchResultView = searchResultView;
this.tabView = tabView;
this.keywordListView = keywordListView;
this.subscribeViewEvents();
this.render();
}
}
์์ ํฌ์คํธ์์๋ ์ค์ค์ด ์ค๋ช ํ๋ฏ์ด ๋น์ฐํ View๋ Controller์ ์์กด๋๋ฏ๋ก keywordListView์ ์์กด์ฑ์ด ์ถ๊ฐ๋๋ค.
์ถ๊ฐ๋ก usbscribeViewEvents์์ ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ์ฒ๋ฆฌ๋ ๊ฒ์ด๊ณ render์๋ keyword list๋ฅผ ๋ ๋๋งํ๋ ์์ ์ด ์ถ๊ฐ๋ ๊ฒ์ผ๋ก ์์์ด ๋ ๊ฒ์ด๋ค.
Model
export default class Store {
constructor(storage) {
if (!storage) throw "no storage";
this.storage = storage;
this.searchResult = [];
this.searchKeyword = "";
this.selectedTab = TabType.KEYWORD;
}
getKeywordList() {} // ์ถ๊ฐ๋ ๊ธฐ๋ฅ
}
Store ๊ฐ์ฒด๋ Persistence Layer์ ๋ฐ์ ํ๋ฏ๋ก ๋ฐ์ดํฐ ์ ๋ณด๋ฅผ ๋ค๊ณ ์๋ค.
๊ทธ๋์ ์ถ์ฒ ๊ฒ์์ด๋ ์ต์ ๊ฒ์์ด์ ๋ํ ์ ๋ณด๊ฐ ์ ์ฅ๋ storage๋ Store์ ์์กด๋๊ณ ์๋ค.
(์ฒซ ๊ฒ์๊ธ๋ถํฐ ์ด๋ฐ ํ ํ๋ฆฟ์ ๋ง์ถ์ด์ ์์ฑํ๋ค๋ฉด.. storage์ ๋ํ ์ค๋ช ์ ์ถฉ๋ถํ ํ์ํ ๋ฐ ์ํ๊น๋ค.)
const storage = {
keywordData: [],
historyData: [],
productData: []
}
๊ฐ์ฌ๋ git์ด๋ ๊ฐ์๋ฅผ ๋ค์ผ๋ฉด ์ ์ ์๊ฒ ์ง๋ง, storage๋ ์ด๋ฐ์์ผ๋ก ์ฌ์ฉ๋ ์ ๋ณด๊ฐ ์ ์ ๋ฐ์ดํฐ๋ก ์ ์ฅ๋์ด์๋ค.
์, ๊ทธ๋์ ์ด๋ฒ ๊ธฐ๋ฅ ๊ตฌํ์์๋ ์ถ์ฒ ๊ฒ์์ด์ ๋ํ keyword๋ค์ ๊ฐ์ ธ์ค๊ธฐ ์ํด model ์์ storage์ ์ ๊ทผํด keyword๋ฅผ ๊ฐ์ ธ์์ผํ๋ค.
Quest
Q. keyword ํด๋ฆญ ์ ๊ฒ์ ๊ฒฐ๊ณผ ๋ถ๋ฌ์ค๊ธฐ
// View
show(keyword) {
this.inputElement.value = keyword;
this.showResetButton(true);
}
// Controller
this.keywordListView
.on("@click", (event) => {
const keyword = event.detail.value;
this.searchFormView.show(keyword);
this.search(keyword);
});
๋๋ ํค์๋๋ฅผ ํด๋ฆญํ ๊ฒฝ์ฐ, View์์ ํ ์คํธ ์ฐฝ์ ๊ธ์ ์ถ๊ฐํ๊ณ reset button์ ๋ง๋ค์ด์ฃผ๋๋ก ํ๋ค.
๊ทผ๋ฐ ์กฐ๊ธ ๋ฉ์ฒญํ๋ค. ์์ ์ ์๋์ ๊ฐ์ด ์ํ์ด ๋์ด์ผ ํ๋ค.
1. ์ฌ์ฉ์๋ Keyword List View์์ ๊ฒ์์ ํ๋ค.
2. Controller๋ก ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์ด์ผ ํ๋ค.
3. ๊ฒ์ํ ํค์๋๊ฐ Store์ ์ ์ฅ์ด ๋์๋ค.
4. ์ ์ฅ์ด ๋ ๋ด์ฉ๋ฌผ์ View์ ๋ฐ์ํ๋ค.
๊ทผ๋ฐ ๋ด๊ฐ ์์ฑํ ๋ก์ง์ 3๋ฒ์ ๊ณ ๋ ค๋ชปํ ๊ฒ์ด ์์ฌ์ ๋ค. ์ด์ ์ ์์ฑํ search ๋ฉ์๋์์ Store ๊ฐ์ฒด์ searchKeyword๋ก keyword ๊ฐ์ ์ ๋ฌํ๋๋ฐ.. ์ด๊ฒ์ ์ด์ฉํด์ render ํ ๋, ์ถ๊ฐํ๋ ๊ฒ์ด ๋ ๊น๋ํด๋ณด์๋ค.
๋ฌผ๋ก , ๋ด ์ฝ๋๋ ๋์ํ์ง๋ง ๋น์ทํ ๊ธฐ๋ฅ๋ค์ ํตํฉ์ฑ์ด๋ผ๊ณ ํด์ผํ ๊น.. ๊ฐ์๊ฐ ๋๋ฌด ๊ด์ฐฎ๋ค.
renderSearchResult() {
this.searchFormView.show(this.store.searchKeyword);
this.tabView.hide();
this.keywordListView.hide();
this.searchResultView.show(this.store.searchResult);
}
.๊ฒ์๊ฒฐ๊ณผ๋ฅผ ๋ ๋๋ง ํ๋ ๋ฉ์๋์ ์์ง๋๊ฐ ์ ๋ง ์ข๋ค.