-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasheesh.js
84 lines (64 loc) · 2.07 KB
/
hasheesh.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
(function(doc, window, $, undefined){
window.hasheesh = {
// Namespace to look at
namespace: null,
// Load function
// Gets called for every hash change
load: function() {
// Clear screen
$( "#container" ).html( "" );
var hashObj = location.hash.slice(1);
if ( typeof hasheesh.namespace[hashObj] === "object" && typeof hasheesh.namespace[hashObj].init === "function" ) {
hasheesh.namespace[hashObj].init();
} else {
var el = doc.createElement('script'),
script = doc.getElementsByTagName('script')[0];
el.src = "js/app/" + hashObj + "/init.js";
script.parentNode.insertBefore(el, script);
if ( !hasheesh.namespace[hashObj] ) {
hasheesh.waitingFor = hashObj;
hasheesh.timeout = setTimeout("hasheesh.loadModule()", 500);
}
}
},
loadModule: function() {
if ( !hasheesh.waitingFor ) {
if ( hasheesh.timeout ) {
clearTimeout( hasheesh.timeout );
}
return;
}
if ( hasheesh.namespace[hasheesh.waitingFor] ) {
var hash = hasheesh.waitingFor;
// Object found, clear first
hasheesh.waitingFor = null;
clearTimeout( hasheesh.timeout );
// Now call init()
hasheesh.namespace[hash].init();
return;
}
hasheesh.timeout = setTimeout("hasheesh.loadModule()", 500);
},
// Helper function to load templates
loadTemplate: function(opts) {
var defaults = {
file: null,
context: "#container",
callback: null
},
options = $.extend(defaults, opts || {});
$.ajax({
url: "js/app/" + options.file + ".html",
dataType: "html",
success: function(template) {
$( options.context ).html( template );
if ( $.isFunction( options.callback) ) {
options.callback();
}
}
});
}
}
// Get the app hooked to hasheesh
window.onhashchange = hasheesh.load;
})(document, window, jQuery);