Good community, I am trying to migrate a project using the require.js library to perform asynchronous modules, but I have encountered problems that I do not know how to solve. Trying to use variables to save templates such as
Index_Ctrl.js
define({
titulo: "Probando AMD con Require.js y Jquery",
cuerpo: "Esto es una muestra de como debe funcionar\n\
require js y jquery, su configuracion, entre otras cosas\n\
un estilo MVC."
});
Index_tpl.js
define(['IndexCtrl'], function (IndexCtrl){
var index_page = {
template: function (){
var titulo, cuerpo;
titulo = IndexCtrl.titulo;
cuerpo = IndexCtrl.cuerpo;
var html = '<h1>'+ titulo +'</h1>'+
'<p>' + cuerpo + '</p>'+
'<button id="btn_menu">Menu</button>';
$('#contenedor').append(html);
}
};
return {
template: index_page.template
};
});
index.html
<html>
<head>
<title>AMD</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="contenedor">
</div>
<script data-main="js/app/main" src="js/libs/require.js"></script>
</body>
</html>
main.js
require.config({
baseUrl: 'js',
paths : {
jquery : ['libs/jquery-1.12.0.min',
'libs/jquery-2.2.0.min'],
IndexTpl: 'app/Index_Tpl',
IndexCtrl: 'app/Index_Ctrl',
MenuTpl: 'app/Menu_Tpl',
MenuCtrl: 'app/Menu_Ctrl'
},
shim: {
IndexTpl: ['jquery'],
MenuTpl: ['jquery']
}
});
define(['IndexTpl', 'MenuTpl'], function (index, menu){
index.template();
});
I would like you to click on a button a module to use another template that are the following
Menu_Tpl.js
define(['MenuCtrl'], function (MenuCtrl){
var menu_page = {
template_m: function (){
var titulo, cuerpo;
titulo = MenuCtrl.titulo;
cuerpo = MenuCtrl.cuerpo;
var html = '<h1>'+ titulo +'</h1>'+
'<p>' + cuerpo + '</p>'+
'<button id="btn_index">Index</button>';
$('#contenedor').append(html);
}
};
return {
template_m: menu_page.template_m
};
});
Menu_Ctrl.js
define({
titulo: "Probando AMD con Require.js y Jquery",
cuerpo: "Este es el menu de prueba."
});