Skip to content

Commit 65473fe

Browse files
committed
refactor: vendingMachine controller initialize 메소드 추가 및 menuItem 관련 로직 분리
1 parent 876a9fd commit 65473fe

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

src/js/app.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import VendingMachineController from "./controller/vendingMachine.js";
22

3-
function vendingMachindApp() {
4-
// eslint-disable-next-line no-unused-vars
5-
const controller = new VendingMachineController();
6-
}
7-
8-
vendingMachindApp();
3+
const controller = new VendingMachineController();
4+
controller.initialize();

src/js/controller/vendingMachine.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ class VendingMachineController {
1818
constructor() {
1919
this.currentMenu = getLocalStorage("menu") || "manager";
2020

21-
const $menu = $("#menu");
22-
const $menuItem = $(`#menu button[name=${this.currentMenu}]`);
2321
const managerModel = new ProductManagerModel();
2422
const chargerModel = new ChangeChargerModel();
2523
const purchaseModel = new ProductPurchaseModel();
@@ -41,12 +39,6 @@ class VendingMachineController {
4139
},
4240
purchase: () => {},
4341
};
44-
45-
this.#currentModel.initialize();
46-
this.#handlers[this.currentMenu]();
47-
48-
$menuItem.classList.add("active");
49-
$menu.addEventListener("click", this.menuHandler.bind(this));
5042
}
5143

5244
submitChargerForm(e) {
@@ -118,6 +110,16 @@ class VendingMachineController {
118110
this.#models[$target.name].initialize();
119111
this.#handlers[$target.name]();
120112
}
113+
114+
bindEventHandlers() {
115+
this.#handlers[this.currentMenu]();
116+
$("#menu").addEventListener("click", this.menuHandler.bind(this));
117+
}
118+
119+
initialize() {
120+
this.#currentModel.initialize();
121+
this.bindEventHandlers();
122+
}
121123
}
122124

123125
export default VendingMachineController;

src/js/view/view.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@ import {
22
createTemplateElement,
33
CONTAINER_TEMPLATES,
44
} from "../utils/templates.js";
5+
import { $, $$ } from "../utils/selector.js";
56

67
class View {
78
constructor(name) {
89
this.name = name;
9-
this.$app = document.querySelector("#app");
1010
this.template = CONTAINER_TEMPLATES[name];
11+
this.$app = document.querySelector("#app");
12+
this.$menuItem = $(`#menu button[name=${this.name}]`);
13+
this.$$buttons = $$("#menu button");
1114
}
1215

13-
render() {
16+
renderMenu() {
17+
this.$$buttons.forEach((button) => {
18+
button.classList.remove("active");
19+
});
20+
21+
this.$menuItem.classList.add("active");
22+
}
23+
24+
renderContent() {
1425
const { content } = createTemplateElement(this.template);
26+
1527
this.$app.replaceChildren(content);
1628
}
29+
30+
render() {
31+
this.renderContent();
32+
this.renderMenu();
33+
}
1734
}
1835

1936
export default View;

0 commit comments

Comments
 (0)