I am learning RequireJS, in the process I am stuck with the below problem:
I am trying to implement a single page application. Below scenario considered is similar to actual problem.
Index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<script src="require.js" data-main="main.js"></script>
</head>
<body>
<button id="start">Start</button>
<div id="buttonContainer"></div>
</body>
</html>
main.js
require([ 'jquery' ], function( $) {
$(function() {
$("#start").click(function() {
$("#buttonContainer").load("page1.html");
require(['module1'],function(){});
});
});
});
page1.html
<div>
<button id="button_1">Click Me!!</button>
</div>
module1.js
define(['jquery'],function($){
$(function(){
$("#button_1").click(function(){
$("#buttonContainer").load("page2.html");
require(['module2'],function(){});
});
});
});
page2.html
<div>
<button id="button_2">Back</button>
</div>
module2.js
define(['jquery'],function($){
$(function(){
$("#button_2").click(function(){
$("#buttonContainer").load("page1.html");
require(['module1'],function(){});
});
});
});
On click of start in index.html the div is populated with Click me!!, on click of Click me!! the div is populated with Back button and on click of Back button again Click me!! is populated and viceversa.
On jquery load of html, I am calling require()..to call respective js function for html. Is my approach correct??