Introduction
I just want someone else to do it, to deal with the JavaScript and CSS because it’s so hard to discipline ourselves to write 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.
- // <a href="http://slnzero.com" target="_blank">Solution Zero, Inc. Lubbock Texas</a>
- /// Troy Locke -- <a href="mailto:[email protected]" target="_blank">[email protected]</a>
- 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" alt="" />');
- // 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();
- });
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.
Read more articles on JavaScript

Vivek KumarPosted Apr 17, 2016, 2:11 PM
Nice one
Kuppurasu NagarajPosted Apr 17, 2016, 4:14 AM
Nice Sharing..
Kumaresh RajalingamPosted Apr 16, 2016, 8:31 AM
Good one
Sr KarthigaPosted Apr 16, 2016, 4:29 AM
good one
Sr KarthigaPosted Apr 16, 2016, 4:28 AM
good one
Troy LockePosted Apr 13, 2016, 10:04 AM
The code is here has been poorly formatted and I can't edit it, you can view the original article with properly formatted source at: http://coaden.net/blog/2016/02/17/the-javascript-module-pattern-with-jquery/
Debasis SahaPosted Apr 12, 2016, 1:21 PM
Good One..
Debasis SahaPosted Apr 12, 2016, 1:21 PM
Good One..
Raja TPosted Apr 12, 2016, 6:39 AM
Nice,Thanks for sharing ..
Humayun Kabir MamunPosted Apr 12, 2016, 1:55 AM
Nice...
NitinPosted Apr 12, 2016, 12:47 AM
nice
Rahul Kumar SaxenaPosted Apr 11, 2016, 10:47 PM
Good show
Kuppurasu NagarajPosted Apr 11, 2016, 2:32 PM
Nice Sharing
Mohammed IbrahimPosted Apr 11, 2016, 1:32 PM
nice