11/********************************************************************************************
22 * What we'll learn
33 * ----------------
4- * How to read data from different HTML input types with Separation of Concerns .
4+ * How and why to create an initialization function .
55 */
66
7- /**
8- * We write methods for calculating the budget in the budgetController, and in the
9- * controller function, we use the function i.e., we call the functions from the
10- * budgetController (using budgetCtrl in controller) and then use them accordingly.
11- */
127
138// Budget Controller
149var budgetController = ( function ( ) { // Code related to handling the budget (data) logic
@@ -60,15 +55,39 @@ var UIController = (function(){ // Code to manipulate the UI
6055} ) ( ) ;
6156
6257
58+ /**
59+ * It is always better to create an initialization function to start the app.
60+ * We can create an init() function inside the controller iife. Check the code below to see
61+ * how the init() is written.
62+ *
63+ * Also, we separate our code inside the controller itself, by separating the events into
64+ * a single and simple function known setupEventListeners, where all the event listeners
65+ * and their code will reside. We will call that function inside our init() function.
66+ */
67+
6368// Global App Controller
6469var controller = ( function ( budgetCtrl , UICtrl ) { // Code related to handling events
6570
66- var DOM = UICtrl . getDOMStrings ( ) ;
71+ var setupEventListeners = function ( ) {
72+ // Get all the DOM classes, id's, etc
73+ var DOM = UICtrl . getDOMStrings ( ) ;
74+
75+ // when we press the .add__btn, we should add the expense/income
76+ document . querySelector ( DOM . inputBtn ) . addEventListener ( "click" , ctrlAddItem ) ;
77+
78+ // when we press the Enter/Return Key, we should add the expense/income
79+ document . addEventListener ( "keypress" , function ( event ) {
80+ // If we press the Enter/Return Key, do the following
81+ if ( event . keyCode === 13 || event . which === 13 )
82+ ctrlAddItem ( ) ;
83+ } ) ;
84+ } ;
85+
6786
6887 var ctrlAddItem = function ( ) {
6988 // 1. Get the field input data
7089 var input = UICtrl . getInput ( ) ;
71- console . log ( input ) ;
90+ // console.log(input); // testing
7291
7392 // 2. Add the item to the budget controller
7493
@@ -81,13 +100,17 @@ var controller = (function(budgetCtrl, UICtrl){ // Code related to handling even
81100 // console.log("Its okay!"); // testing
82101 } ;
83102
84- // when we press the .add__btn, we should add the expense/income
85- document . querySelector ( DOM . inputBtn ) . addEventListener ( "click" , ctrlAddItem ) ;
103+ // to be able to call the init() function from the global scope, we return an
104+ // object that contains the init() function as follows:
105+ return {
106+ init : function ( ) {
107+ console . log ( "Application has started." ) ; // test
108+ setupEventListeners ( ) ;
109+ }
110+ } ;
86111
87- // when we press the Enter/Return Key, we should add the expense/income
88- document . addEventListener ( "keypress" , function ( event ) {
89- if ( event . keyCode === 13 || event . which === 13 ) // If we press the Enter/Return Key
90- ctrlAddItem ( ) ;
91- } ) ;
92-
93- } ) ( budgetController , UIController ) ;
112+ } ) ( budgetController , UIController ) ;
113+
114+
115+ // This is the only piece of code that we write in the global scope
116+ controller . init ( ) ;
0 commit comments