1+ class Controller {
2+ constructor ( model , view ) {
3+ this . model = model ;
4+ this . view = view ;
5+ this . init ( ) ;
6+ }
7+
8+ init ( ) {
9+ this . addHandlerForTask ( ) ;
10+ this . addHandlerForRemove ( ) ;
11+ this . addHandlerForUpdate ( ) ;
12+ }
13+
14+ addHandlerForTask ( ) {
15+ const input = this . view . elements . taskField ;
16+ const addButton = this . view . elements . addButton ;
17+
18+ const handlerForAddButton = ( ) => {
19+ if ( input . value . length < 3 ) {
20+ alert ( 'Your length of input is so short' ) ;
21+ }
22+
23+ this . model . addTodoItem ( input . value ) ;
24+ this . view . render ( this . model . data ) ;
25+
26+ input . value = '' ;
27+ }
28+
29+ addButton . addEventListener ( 'click' , handlerForAddButton ) ;
30+ }
31+
32+ addHandlerForRemove ( ) {
33+ const input = this . view . elements . removeField ;
34+ const removeBtn = this . view . elements . removeBtn ;
35+
36+ const handlerForRemoveButton = ( ) => {
37+ this . model . rempveItem ( input . value ) ;
38+ this . view . render ( this . model . data ) ;
39+
40+ input . value = '' ;
41+ } ;
42+
43+ removeBtn . addEventListener ( 'click' , handlerForRemoveButton )
44+ }
45+
46+ addHandlerForUpdate ( ) {
47+ const oldInput = this . view . elements . updateOld ;
48+ const newInput = this . view . elements . updateNew ;
49+ const updateBtn = this . view . elements . updateBtn ;
50+
51+ const handlerForUpdateButton = ( ) => {
52+ this . model . updateItem ( oldInput . value , newInput . value ) ;
53+ this . view . render ( this . model . data ) ;
54+
55+ oldInput . value = '' ;
56+ newInput . value = '' ;
57+ } ;
58+
59+ updateBtn . addEventListener ( 'click' , handlerForUpdateButton )
60+ }
61+ }
0 commit comments