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
|
'use strict';
jQuery(function($, undefined){
var DEBUG = 1,
lscache,
Mustache,
$runButtons,
generateKeyFromPath = function(path){
return 'fetched:' + path;
},
fetchTemplate = function(path, $cb){
var key = generateKeyFromPath(path);
$.get(path, function(template){
console.debug('run $.get');
lscache.set(key, template, 1);
return $cb.fire(template);
});
},
getTemplate = function(path, $cb){
var key = generateKeyFromPath(path);
var template = lscache.get(key);
if (template) {
return $cb.fire(template);
}
else {
fetchTemplate(path, $cb);
}
},
render = function(targetId, targetName){
var obj = {
id: targetId,
name: targetName
};
var $cb = $.Callbacks();
$cb.add(function(template){
var rendered = Mustache.render(template, obj);
$('#' + targetId).html(rendered);
});
getTemplate('templates/init.mst', $cb);
},
changeName = function(e){
var $this = $(e.currentTarget);
render($this.data('id'), $this.html());
},
setVars = function(){
lscache = window.lscache;
Mustache = window.Mustache;
$runButtons = $('button[data-run=changeName]');
},
initHandlers = function(){
$runButtons.on('click', changeName);
},
init = function(){
setVars();
initHandlers();
if (DEBUG) {
lscache.flush();
}
};
init();
});
|