0

can someone please explain this bit in my jQuery book? I don't understand the purpose of the variable initalised.

Here is some explanation in the book:

the taskController remembers if it has been initalized in a local variable called initialised.This ensures that regardless of how many times the init method is called, it will only actually initialize the controller once

jQuery code:

tasksController = function() { 
    var taskPage;
    var initialised = false;

    return { 
        init : function(page) { 
            if (!initialised) {
                taskPage = page;
                $(taskPage).find('[required="required"]').prev('label').append( '<span>*</span>').children( 'span').addClass('required');
                $(taskPage).find('tbody tr:even').addClass('even');

                $(taskPage).find('#btnAddTask').click(function(evt) {
                    evt.preventDefault();
                    $(taskPage).find('#taskCreation').removeClass('not');
                });

                $(taskPage).find('tbody tr').click(function(evt) {
                    $(evt.target).closest('td').siblings().andSelf().toggleClass('rowHighlight');
                });

                $(taskPage).find('#tblTasks tbody').on('click', '.deleteRow', function(evt) { 
                    evt.preventDefault();
                    $(evt.target).parents('tr').remove(); 
                });

                $(taskPage).find('#saveTask').click(function(evt) {
                    evt.preventDefault();
                    if ($(taskPage).find('form').valid()) {
                        var task = $('form').toObject();
                        $('#taskRow').tmpl(task).appendTo($(taskPage).find('#tblTasks tbody'));
                    }
                });
                initialised = true;
            }
        } 
    } 
}();
1
  • like the book said they want the controller to be initialized only once. hence they set a flag to true after the first time the controller is initialized using the init method. after that even when the init method is called multiple times the initialization code won't be executed because the value of the initialized variable. hope this helps Commented Apr 15, 2016 at 4:03

1 Answer 1

1

When this controller is initialized, the "initialised" variable is set to false. Upon calling the init function within the controller, the first line in it checks that the "initialised" variable is not true (and it's not, it's false), then executes the remainder of that code block. At the very end of that block, you can see that this variable is set to true.

This means that the next time you run this function, the local variable is going to have a value of true this time, so when you call the init function, it will re-evaluate that statement first. Since this time "initialized" is true, the check for "is the initialised variable not true" will be false, so the code within that block will not execute.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.