I just started learning Require.js
the file system is like :
This is my index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="lib/require/require.min.js" data-main="lib/main"></script>
</head>
<body>
<span id="content"></span>
</body>
</html>
Now, this I knows that the 1st file that loaded on to the DOM is require.js then after that it loads lib/main.js
Now the main.js is
require(['jquery'],function($){
//this works since both **main.js** and **jquery.js** are in same folder
$("#content").html("jquery am loaded");
});
Now here is the problem if I keep jquery.js in the same folder as that in main.js the code works fine, but if I change the path to jquery/jquery.js and change the main.js as
require(['jquery/jquery'],function($){
//this thing shows 'Uncaught TypeError: undefined is not a function'
$("#content").html("jquery am loaded");
});
I understood the problem is that it is not loading the jquery.js if it is inside any other folder other that main.js is, but why, please shed some light and how that can be achieved.
To use RequireJS and jQuery, you should either use the combined RequireJS/jQuery file, available at:
http://requirejs.org/docs/jquery.html
Or use a path
.
http://requirejs.org/docs/api.html#config-paths
require.config({
paths: {
"jquery": 'http://code.jquery.com/jquery-1.9.1'
}
});
require(["jquery"], function ($) {
console.log($.fn.jquery);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With