Skip to content

Commit 7c3933b

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

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/js/app.js

+2-6
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

+10-8
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

+11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ 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;
910
this.$app = document.querySelector("#app");
11+
this.$menuItem = $(`#menu button[name=${this.name}]`);
12+
13+
this.$$buttons = $$("#menu button");
1014
this.template = CONTAINER_TEMPLATES[name];
1115
}
1216

1317
render() {
1418
const { content } = createTemplateElement(this.template);
19+
1520
this.$app.replaceChildren(content);
21+
22+
this.$$buttons.forEach((button) => {
23+
button.classList.remove("active");
24+
});
25+
26+
this.$menuItem.classList.add("active");
1627
}
1728
}
1829

0 commit comments

Comments
 (0)