OJET4: RequireJS
#####################demo.html####################################
########################main.js#################################
##########################demo.js###############################
##########################jqueryScript.js###############################
##########################mathUtils.js###############################
##########################random100.js###############################
#########################################################
#####################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