JavaScript/Vanila

[Vanila js] ํ‚ค์›Œ๋“œ ๊ฒ€์ƒ‰ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿณ Laboon 2024. 8. 2. 00:15

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);
  }

.๊ฒ€์ƒ‰๊ฒฐ๊ณผ๋ฅผ ๋ Œ๋”๋ง ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ์‘์ง‘๋„๊ฐ€ ์ •๋ง ์ข‹๋‹ค.