-
Notifications
You must be signed in to change notification settings - Fork 0
/
libreria.js
57 lines (57 loc) · 1.96 KB
/
libreria.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use strict";
(function (window, document){
var inicio = function(){
var elemento = null,
marco = null,
rutas = {},
controladores = {},
controlador,
libreria = {
getID: function(id){
elemento = document.getElementById(id);
return this;
},
noSubmit: function(){
elemento.addEventListener("submit", function(e){
e.preventDefault();
}, false);
return this;
},
enrutar: function(){
marco = elemento;
return this;
},
ruta: function(ruta, plantilla, controlador, carga){
rutas[ruta] = {
"plantilla": plantilla,
"controlador": controlador,
"carga": carga
};
return this;
},
manejadorRutas: function(){
var hash = window.location.hash.substring(1) || '/',
destino = rutas[hash],
xhr = new XMLHttpRequest();
if(destino && destino.plantilla){
xhr.addEventListener("load", function (){
marco.innerHTML = this.responseText;
}, false);
xhr.open("get", destino.plantilla, true);
xhr.send(null);
}else{
window.location.hash = "#/";
}
}
};
return libreria;
};
if(typeof window.libreria === "undefined"){
window.libreria = window._ = inicio();
window.addEventListener("load", libreria.manejadorRutas, false);
window.addEventListener("hashchange", libreria.manejadorRutas, false);
}
else{
console.log("Se Esta llamando la libreria nuevamente");
}
})(window, document);