1

I was doing a lot of mocking of a layout page. I wrote lots of javascript using $(document).ready in the header to test.

$(document).ready(function () {
    $('.errorPopup').hide();
    $('#account').mask('9999-9999-9999-9999');
    $('#code').mask('9999');

    $("#form").validate({
                errorPlacement: function (error, element) {
                    $('.errorPopup').show();

        },
        success: function (label, element) {
            $('.errorPopup').hide();
        },
        rules: {           
            "account": {
                required: true
                },
            "code": {
                required: true,
                max:1000
            }
        },
        messages: {
            "account": {
                required: "Account is required."
            },
            "code": {
                required: "Code is required",
                max: "Sorry only 1000 max"

            }
        },
        submitHandler: function (form) { 
            return false; 
        }
    });
});

Now I have to add it to the main.js file that site use and everything is organized in class/objects.

//main.js

var checkAccount = {
     init: function(holder){
     messages = holder;
     $('elment1').click(//do something);
     $('elment2').click(//do something);
     },
     someAction: function(misc) ...  
}

How would I write my stuff in that format? How would I have it behave like before in $document.ready()?

THanks

4
  • The short of it is you make it into a class/object and initialize it on doc ready
    – Huangism
    Jul 30, 2014 at 15:20
  • I am not the best person to give you an answer because my understanding of it is not that deep
    – Huangism
    Jul 30, 2014 at 15:44
  • Why don't you just put your checkAccount inside $(document).ready ?
    – Bla...
    Jul 30, 2014 at 16:13
  • how would i structure the object? init is like the constructor? Can you write an answer showing how you'd do it exactly
    – Proximo
    Jul 30, 2014 at 16:22

0

Your Answer

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

Browse other questions tagged or ask your own question.