Skip to content

Commit

Permalink
refactor: vendingMachine controller initialize 메소드 추가 및 menuItem 관련 로…
Browse files Browse the repository at this point in the history
…직 분리
  • Loading branch information
go-lani committed Jan 31, 2023
1 parent 876a9fd commit 4d23046
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
8 changes: 2 additions & 6 deletions src/js/app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import VendingMachineController from "./controller/vendingMachine.js";

function vendingMachindApp() {
// eslint-disable-next-line no-unused-vars
const controller = new VendingMachineController();
}

vendingMachindApp();
const controller = new VendingMachineController();
controller.initialize();
18 changes: 10 additions & 8 deletions src/js/controller/vendingMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class VendingMachineController {
constructor() {
this.currentMenu = getLocalStorage("menu") || "manager";

const $menu = $("#menu");
const $menuItem = $(`#menu button[name=${this.currentMenu}]`);
const managerModel = new ProductManagerModel();
const chargerModel = new ChangeChargerModel();
const purchaseModel = new ProductPurchaseModel();
Expand All @@ -41,12 +39,6 @@ class VendingMachineController {
},
purchase: () => {},
};

this.#currentModel.initialize();
this.#handlers[this.currentMenu]();

$menuItem.classList.add("active");
$menu.addEventListener("click", this.menuHandler.bind(this));
}

submitChargerForm(e) {
Expand Down Expand Up @@ -118,6 +110,16 @@ class VendingMachineController {
this.#models[$target.name].initialize();
this.#handlers[$target.name]();
}

bindEventHandlers() {
this.#handlers[this.currentMenu]();
$("#menu").addEventListener("click", this.menuHandler.bind(this));
}

initialize() {
this.#currentModel.initialize();
this.bindEventHandlers();
}
}

export default VendingMachineController;
20 changes: 19 additions & 1 deletion src/js/view/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,36 @@ import {
createTemplateElement,
CONTAINER_TEMPLATES,
} from "../utils/templates.js";
import { $, $$ } from "../utils/selector.js";

class View {
constructor(name) {
this.name = name;
this.$app = document.querySelector("#app");
this.$menuItem = $(`#menu button[name=${this.name}]`);

this.$$buttons = $$("#menu button");
this.template = CONTAINER_TEMPLATES[name];
}

render() {
renderMenu() {
this.$$buttons.forEach((button) => {
button.classList.remove("active");
});

this.$menuItem.classList.add("active");
}

renderContent() {
const { content } = createTemplateElement(this.template);

this.$app.replaceChildren(content);
}

render() {
this.renderContent();
this.renderMenu();
}
}

export default View;

0 comments on commit 4d23046

Please sign in to comment.