Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

The JavaScript Module Pattern With jQuery

4.92/5 (10 votes)
10 Feb 2016MIT2 min read 39.7K  
The JavaScript Module Pattern used with jQuery

Introduction

I have always been primarily a backend developer, I love OOP, and try my best to follow all the best principles such as Encapsulation, Polymorphism, Separation of Concerns, and even the Law of Demeter when I design and write software. As such, I have fought tooth and nail to avoid writing in-browser apps. I have nothing against them, I believe that’s where the View needs to be... philosophically. I just want someone else to do it, to deal with the JavaScript and CSS because it’s so hard to disciple ourselves to writing good, clean code. OOP code in the browser with JavaScript ES5 isn't difficult to write correctly, it’s just easy not to. (In future articles, I’ll discuss how I’ve overcome this with Angular 2, Typescript, and even ES6 features)

Background

Here we introduce the Module Pattern, this gives us a way in JavaScript to introduce private variables and functions, exposing only those parts we need to the outside world. There are several flavors of this available, you can implement it as a JavaScript object, you can use prototypes, or you can write it as an IIFE a JavaScript Immediately Invoked Function Expression. To do this, we implement a JavaScript Closure. More about closures here.

Using the Code

Enjoy the sample, and remember, it’s just a sample as each case may call for something a little different. For example, I’ve separated Init() and showMessage() functionality which in many cases can be combined.
Note: This code is not designed to be functional but to be used as a template.

JavaScript
/// JavaScript source code representing example jQuery aware module pattern
/// Solution Zero, Inc. Lubbock Texas 
/// Troy Locke -- troy@slnzero.com
var myMessageApp = (function() {
    "use strict"

    // I avoid these with the bindControls functionality but I show if for example.
    var someElement = $("#foo"); // some element I know I'll use lots

    // private variables
    var pvtMessageVal;
    var pvtAdditionalMessageVal;
    
    // we create an object to hold all the jQuery controls, so we can call
    // binding after loading an HTML page dynamically via AJAX
    // see bindControls further down
    var messageCtrls = {};

    var config = {
        // *example, this must be passed into init(config)
        fooSelector: null, // $("#foo")
        messageSelector: null, // $(".message")
        additionalMessageSelector: null, // $(".additional_message")
        options: {
            showOK: true,
            showCancel: true,
            warningLevel: 1,
        }
    }

    // AJAX calls
    var getMessage = function(message) {
        $.ajax({
            url: '/getMessagePage',
            type: 'POST',
            dataType: "json",
            data: {'message' : message},
            success: function(data) {
                // ...
                messageCtrls.mainMessageDiv.html(data.message);
                // call bind controls to bind to the newly introduced dom elements
                messageCtrls = bindMessageControls();
                },
            error: function() {
                // ...
                }
        });
    };

    var inputClick = function(event) {
        event.preventDefault();
        // depending on if you'll reuse these selectors throughout 
        // the app I might have these as variables
        $('.loading').html('<img class="remove_loading" src="/graphics/loading.gif" />');

        // try to avoid these
        var msg = $(".additionalMessage").val();
        // and use this 
        var msg = config.additonalMessageSelector.val();
        // or
        var msg = pvtAdditionalMessageVal;

        if (msg == ""){
            $("#message_empty").jmNotify();
            $('.remove_loading').remove();
        } else {
            getMessage(msg);
        }
    };

    var bindMessageControls = function () {
        var self = {};

        // Modal
        self.thisModal = $(".MessageModal");

        // CheckBoxs
        self.fooCb = $(".foo_checkbox");
        
        // Buttons
        self.okBtn = $(".btnOk");
        self.cancelBtn = $(".btnCancel");

        // Divs
        self.mainMessageDiv = $(".main_message");
        self.additionalMessageDiv = $(".addtional_message");

        //Help Icons
        self.HelpIcon = $(".help-icon");

        return self;
    };

    var bindVals = function () {
        //check to make sure we have a valid config passed in before we set the values
        if (!config.messageSelector) throw "Invalid configuration object passed in init()";
        
        //bind the values to "private variables"
        pvtMessageVal = config.messageSelector.val();
        
        //this control is optional, test existence
        if(config.additionalMessageSelector.length)
            pvtAdditionalMessageVal = config.additionalMessageSelector.val();
    };

    var bindFunctions = function() {
        // you can use jQuery
        $("btnOk").on("click", inputClick)
        // but we have the controls object to use, so instead
        messageCtrls.okBtn.on('click, inputClick')
    };

    var init = function () {
        messageCtrls = bindMessageControls();
        bindFunctions();
    };

    var showMessage = function (cfg) {
        config = cfg;
        bindVals();
        messageCtrls.thisModal.modal({
            show: true,
            keyboard: false,
            backdrop: "static"
        });
    };
    
    return {
        init: init,
        show: showMessage,
        getMessage: getMessage
        //anything else you want available
        //through myMessageApp.function()
        //or expose variables here too
    };

})();
 
//usage
$("document").ready(function () {
    myMessageApp.init();
});

Points of Interest

This is the first in a series that will explore the Module Pattern in JavaScript. In the next part, I will break down the code in this example and explain in detail the whats and whys. Then I hope to show examples of other implementation of this pattern using objects, prototypes, and other variations such as the Revealing Module Pattern.

History

I'm a backend developer by trade moving into the in browser arena, so my post tends to be an effort to find a way to force structure on web development. If anyone else struggles in this area, please feel free to contact me.

License

This article, along with any associated source code and files, is licensed under The MIT License