File tree 3 files changed +23
-14
lines changed
3 files changed +23
-14
lines changed Original file line number Diff line number Diff line change 1
1
import VendingMachineController from "./controller/vendingMachine.js" ;
2
2
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 ( ) ;
Original file line number Diff line number Diff line change @@ -18,8 +18,6 @@ class VendingMachineController {
18
18
constructor ( ) {
19
19
this . currentMenu = getLocalStorage ( "menu" ) || "manager" ;
20
20
21
- const $menu = $ ( "#menu" ) ;
22
- const $menuItem = $ ( `#menu button[name=${ this . currentMenu } ]` ) ;
23
21
const managerModel = new ProductManagerModel ( ) ;
24
22
const chargerModel = new ChangeChargerModel ( ) ;
25
23
const purchaseModel = new ProductPurchaseModel ( ) ;
@@ -41,12 +39,6 @@ class VendingMachineController {
41
39
} ,
42
40
purchase : ( ) => { } ,
43
41
} ;
44
-
45
- this . #currentModel. initialize ( ) ;
46
- this . #handlers[ this . currentMenu ] ( ) ;
47
-
48
- $menuItem . classList . add ( "active" ) ;
49
- $menu . addEventListener ( "click" , this . menuHandler . bind ( this ) ) ;
50
42
}
51
43
52
44
submitChargerForm ( e ) {
@@ -118,6 +110,16 @@ class VendingMachineController {
118
110
this . #models[ $target . name ] . initialize ( ) ;
119
111
this . #handlers[ $target . name ] ( ) ;
120
112
}
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
+ }
121
123
}
122
124
123
125
export default VendingMachineController ;
Original file line number Diff line number Diff line change @@ -2,17 +2,28 @@ import {
2
2
createTemplateElement ,
3
3
CONTAINER_TEMPLATES ,
4
4
} from "../utils/templates.js" ;
5
+ import { $ , $$ } from "../utils/selector.js" ;
5
6
6
7
class View {
7
8
constructor ( name ) {
8
9
this . name = name ;
9
10
this . $app = document . querySelector ( "#app" ) ;
11
+ this . $menuItem = $ ( `#menu button[name=${ this . name } ]` ) ;
12
+
13
+ this . $$buttons = $$ ( "#menu button" ) ;
10
14
this . template = CONTAINER_TEMPLATES [ name ] ;
11
15
}
12
16
13
17
render ( ) {
14
18
const { content } = createTemplateElement ( this . template ) ;
19
+
15
20
this . $app . replaceChildren ( content ) ;
21
+
22
+ this . $$buttons . forEach ( ( button ) => {
23
+ button . classList . remove ( "active" ) ;
24
+ } ) ;
25
+
26
+ this . $menuItem . classList . add ( "active" ) ;
16
27
}
17
28
}
18
29
You can’t perform that action at this time.
0 commit comments