-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathICanHaz.load.js
77 lines (65 loc) · 1.98 KB
/
ICanHaz.load.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
(function($, ich) {
/*
* ICanHaz.load.js
*
* Usage:
*
* ich.load('template', function() {
* // Executed as soon as template loading has finished
* var $tpl = ich.template({
* foo: 'bar'
* });
* });
*
*
* by Finn-Lennart Heemeyer, License: MIT
*/
var clearAll = ich.clearAll,
templates = {};
ich.load = function(templateName, cb, templatePath, templateExtension) {
if (!templates[templateName]) {
// Save template loading state und all callbacks here
templates[templateName] = {
ready: false,
callbacks: [cb]
};
var path = [(templatePath || ich.templatePath), '/',
templateName, '.',
(templateExtension || ich.templateExtension || 'mustache')].join('');
// Request template file async
$.get(path, function(data) {
// Add template to use in callbacks
ich.addTemplate(templateName, data);
// Mark template as loaded
templates[templateName].ready = true;
// Execute all callbacks
for (var i = 0; i < templates[templateName].callbacks.length; i++) {
templates[templateName].callbacks[i]();
}
});
} else if (templates[templateName].ready) {
// If template has been loaded before, execute callback
cb();
} else {
// If template loading was started, add cb to callbacks
templates[templateName].callbacks.push(cb);
}
};
/*
* Fast version: Do everything in one function
*/
ich.loadTemplate = function(templateName, data, cb, templatePath, templateExtension) {
ich.load(templateName, function() {
// Compute template from data, execute callback
var content = ich[templateName](data);
cb(content);
}, templatePath, templateExtension);
};
/*
* Make sure to also clean up templates object when calling clearAll()
*/
ich.clearAll = function() {
templates = {};
clearAll();
};
})(window.jQuery, window.ich);