-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhgn.js
84 lines (72 loc) · 2.93 KB
/
hgn.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
/**@license
* RequireJS Hogan Plugin | v0.3.0 (2013/06/11)
* Author: Miller Medeiros | MIT License
*/
define(['hogan', 'text'], function (hogan, text) {
var DEFAULT_EXTENSION = '.mustache';
var _buildMap = {};
var _buildTemplateText = 'define("{{pluginName}}!{{moduleName}}", ["hogan"], function(hogan){'+
' var tmpl = new hogan.Template({{{fn}}}, "", hogan);'+
// need to use apply to bind the proper scope.
' function render(){ return tmpl.render.apply(tmpl, arguments); } render.template = tmpl; return render;'+
'});\n';
var _buildTemplate;
function load(name, req, onLoad, config){
var hgnConfig = config.hgn || {};
var fileName = name;
fileName += hgnConfig && hgnConfig.templateExtension != null? hgnConfig.templateExtension : DEFAULT_EXTENSION;
// load text files with text plugin
text.get(req.toUrl(fileName), function(data){
var compilationOptions = hgnConfig.compilationOptions? mixIn({}, hgnConfig.compilationOptions) : {};
if (config.isBuild) {
// store compiled function if build
// and should always be a string
compilationOptions.asString = true;
_buildMap[name] = hogan.compile(data, compilationOptions);
}
// maybe it's required by some other plugin during build
// so return the compiled template even during build
var template = hogan.compile(data, compilationOptions);
var render = bind(template.render, template);
// add text property for debugging if needed.
// it's important to notice that this value won't be available
// after build.
render.text = template.text;
render.template = template;
// return just the render method so it's easier to use
onLoad( render );
});
}
function bind(fn, context) {
return function(){
return fn.apply(context, arguments);
};
}
function mixIn(target, source) {
var key;
for (key in source){
if ( Object.prototype.hasOwnProperty.call(source, key) ) {
target[key] = source[key];
}
}
return target;
}
function write(pluginName, moduleName, writeModule){
if(moduleName in _buildMap){
if (! _buildTemplate) {
// using templates to generate compiled templates, so meta :P
_buildTemplate = hogan.compile( _buildTemplateText );
}
var fn = _buildMap[moduleName];
writeModule( _buildTemplate.render({
pluginName : pluginName,
moduleName : moduleName,
fn : fn
}) );
}
}
return {
load : load,
write : write
};
});