• 카테고리

    질문 & 답변
  • 세부 분야

    프론트엔드

  • 해결 여부

    미해결

reset() 함수 작성 관련 문의드립니다.

23.01.14 10:38 작성 조회수 193

0

안녕하세요.

저는 아래와 같이 Controller에서는 Store의 reset()함수를 호출만하고, Store에서 상태를 변경하는 방식으로 작성했는데요.

// Controller.js

export default class Controller {
  constructor(store, { searchFormView, searchResultView }) {
    this.store = store;

    this.searchFormView = searchFormView;
    this.subscribeViewEvents();
  }

  subscribeViewEvents() {
    this.searchFormView //
      .on("@reset", () => this.reset());
  }

  reset() {
    console.log(tag, "reset");
    this.store.reset(); // 작성한 부분
    this.render();
  }
}
// Store.js

export default class Store {
  constructor(storage) {
    console.log(tag, "constructor");

    if (!storage) throw "no storage";

    this.storage = storage;

    this.searchKeyword = "";
    this.searchResult = [];
  }

  // 작성한 부분
  reset() {
    this.searchKeyword = "";
    this.searchResult = [];
  }
}

강사님 풀이는 다음과 같이, Controller에서 store의 상태값을 직접 넣어주는 식으로 작성을 하셨더라구요.

// Controller.js

...

reset() {
    console.log(tag, "reset");

    this.store.searchKeyword = "";
    this.store.searchResult = [];
    this.render();
  }

둘 다 기능상의 차이는 없는 것 같은데, 혹시 둘 중 선호되는 방식이 있는지 궁금합니다.

답변 1

답변을 작성해보세요.

3

저는 컨트롤러에서 직접 스토어 내부 변수를 초기화했는데요. 이렇게 스토어 메소드로 분리하니깐 역할분담이 더 잘된것 같네요. 저는 JIYEON KIM 님 코드가 더 좋습니다.

JIYEON KIM님의 프로필

JIYEON KIM

질문자

2023.01.19

답변 감사합니다!