modular design

by ankeetmaini

HTML

<div class="container">
    	<h2>This is a demonstration of <em>Modular JavaScript</em> design.</h2>

    <p>We create a global module called <em>AppleApp</em> which has a private variable called <em>apples</em>. Using modular design we'll see how we keep <em>apples private</em> and use <em>public methods</em> to access it.</p>
    <hr>
    	<h3>The <em>AppleApp</em></h3>

    <p>The <em>AppleApp</em> is pretty simple, <em>apples</em> is the private variable that keeps the number of apples with you.</p>
    <p>To access, modify the <em>apples</em> we use these methods.
        <ol>
            <li class="private">eatApple() - Reduces the stock by one - Private method</li>
            <li class="private">buyApples(num) - Increases the stock by <em>num</em> - Private method</li>
            <li class="public">viewApples() - Shows the current stock of apple - Public Method</li>
        </ol>
    </p>
    <hr>
    <div class="app center">	<span><h3 style="display: inline;">apples</h3></span><span class="box"></span>

        <hr style="width: 60%; margin: 8px auto; border-style: dashed;">
        <input type="button" id="eatApple" value="eatApple()" style="margin-right: 80px;">
        <input type="number" id="numberOfApples" min="1" value="1" style="width: 45px;">
        <input type="button" id="buyApples" value="buyApples()">
    </div>
</div>

CSS

.container {
    width: 50%;
    margin: 0 auto;
}
.center {
    text-align: center;
}
.box {
    border: 1px solid maroon;
    padding: 3px 8px;
    margin-left: 10px;
    font-weight: bold;
    background: yellow;
}
.public {
    color: green;
    font-weight: bold;
}
.private {
    color: red;
    font-weight: bold;
}

JavaScript

var AppleApp = (function ($) {
    //private variables and methods
    var apples = 0;
    var eatApple = function () {
        if (apples > 0) {
            apples--;
        } else {
            alert("Cannot eat. No stock. Buy first.");
        }
        $(document).trigger("ateApple.appleApp");
    };
    var buyApples = function (newApples) {
        apples += parseInt(newApples, 10);
        $(document).trigger("addedApples.appleApp");
    };
    var bindDOMEvents = function () {
        //on button clicks
        $('input#eatApple').on("click", function () {
            eatApple();
        });
        $('input#buyApples').on("click", function () {
            var num = $('#numberOfApples').val();
            buyApples(num);
        });
        //to display current value of apples
        $(document).on("addedApples.appleApp ateApple.appleApp", function () {
            $('span.box').text(apples);
        });
        //to change the label of button
        $('#numberOfApples').on("change", function () {
            $('input#buyApples').attr('value', 'buyApples(' + $(this).val() + ')');
        });

    };

    //our own document.ready function
    (function () {
        $('span.box').text(apples);
        $('input#buyApples').attr('value', 'buyApples(' + $('#numberOfApples').val() + ')');
        bindDOMEvents();
    }());

    //public methods
    return {
        viewApples: function () {
            return apples;
        }
    };
}(jQuery));