Skip to content

Commit 068b98d

Browse files
authored
Create router.js
1 parent 92f4234 commit 068b98d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/boot/router.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* ENRUTADOR BASADO EN HASH '#vista=[...]' */
2+
3+
define(function () {
4+
5+
window.router = {
6+
src: window.src ? window.src : "src",
7+
actualizar: function (def) {
8+
var vista = _getNombreVista(def);
9+
this.cargar(vista);
10+
},
11+
cargar: function (vista) {
12+
if (!vista) {
13+
return;
14+
}
15+
16+
var url = window.src + '/modulos/vista/' + vista + '/' + vista + '.html';
17+
18+
require(["vue!" + url], function (vista) {
19+
window.app = new Vue(vista).$mount('#app > *:first-child');
20+
});
21+
}
22+
}
23+
24+
//DEFINIR COMO FUNCION
25+
return function () {
26+
//HASH CHANGE EVENT
27+
$(window).on("hashchange", function () {
28+
window.router.actualizar();
29+
});
30+
31+
//AUTO-INICIALIZAR:
32+
window.router.actualizar('inicio');
33+
};
34+
35+
//OBTENER EL MÓDULO VISTA
36+
function _getNombreVista(def) {
37+
38+
var hash = _getUrlParameter(location.hash, "view") || def;
39+
if (!hash) {
40+
return;
41+
}
42+
43+
//AÑADIR CLASE 'vista_'
44+
//https://stackoverflow.com/questions/2644299/jquery-removeclass-wildcard
45+
$("html").removeClass(function (index, className) {
46+
return (className.match(/(^|\s)vista_\S+/g) || []).join(' ');
47+
});
48+
$("html").addClass("vista_" + hash);
49+
50+
return hash;
51+
}
52+
53+
//OBTENER PARAMETRO DEL QUERY URL
54+
//https://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js
55+
function _getUrlParameter(str, sParam) {
56+
var sPageURL = decodeURIComponent(str.substring(1)),
57+
sURLVariables = sPageURL.split('&'),
58+
sParameterName,
59+
i;
60+
61+
for (i = 0; i < sURLVariables.length; i++) {
62+
sParameterName = sURLVariables[i].split('=');
63+
64+
if (sParameterName[0] === sParam) {
65+
return sParameterName[1] === undefined ? true : sParameterName[1];
66+
}
67+
}
68+
}
69+
});

0 commit comments

Comments
 (0)