Thursday, October 11, 2018

OJET4: RequireJS

OJET4: RequireJS 



#####################demo.html####################################

<html>

    <head>
        <script type="text/javascript" src="../require.js" data-main="main"></script>
        <script type="text/javascript">
        alert('Hi')
        </script>
        <!--<script type="text/javascript" src="main.js"></script>-->
    </head>
    <body>
        First Program
    </body>
</html>

########################main.js#################################

require.config({
    baseUrl:'.',
    paths:{
        jquery: 'libs/jquery-3.3.1',
        randomLib: 'libs/random100',
        mathUtils: 'mathUtils'
    }

})

require(['demo', 'jqueryScript', 'mathUtils','randomLib'], function(a, meth, mathObj,rondomFn){

    a();

    meth.updateHTML("Asynchronous Module Definition");

    console.log("mathObj.add(20 + 10) : ",mathObj.add(20,10));
    console.log("mathObj.add(20 - 10) : ",mathObj.subtract(20,10));

    console.log("rondomFn.getRandom() : ",rondomFn());

})

##########################demo.js###############################

define([],

    function(){
        var msg = 'Hello World!'

        function greetings(){
            return console.log(msg)
        }

        return greetings
    })


##########################jqueryScript.js###############################

define(['jquery'], function($){

    var methods = {};

    methods.updateHTML= function(args){
        $('body').html(args);
    };

    return methods;
});



##########################mathUtils.js###############################

define([],function(){ //per JS only 1 define

//function will be called once all depenency is loaded

var math = {};

math.add = function(a,b){
return a+b;
}

math.subtract = function(a,b){
return a-b;
}

math.multiply = function(a,b){
console.log("a : ",a);
console.log("b : ",b);
return a*b;
}

return math;

});

##########################random100.js###############################

define(['mathUtils'],function(mathUtils){

function getRandom(){
//i = parseInt(Math.random()*100);
i = parseInt(mathUtils.multiply(Math.random(), 100));
return i;
}

return getRandom;

});
#########################################################



No comments:

Post a Comment

Custom Java Logic

Custom Java Logic 1. Remove Particular Html tag from String. Requirement - Remove <font> tags, even if it has multiple then rem...