-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloadKoTemplate.js
63 lines (55 loc) · 2.26 KB
/
loadKoTemplate.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
/*globals define */
define(["text", "knockout", "stringTemplateEngine"], function (text, ko) {
var inferTemplateName = function(name) {
var templateName,
index = name.indexOf("!"),
parts;
if (index !== -1) {
//use the template name that is specified
templateName = name.substring(index + 1, name.length);
}
else {
//use the file name sans the path as the template name
parts = name.split("/");
templateName = parts[parts.length - 1].split(".").join("-");
}
return templateName;
},
loader = {
load: function (name, req, load, config) {
var onLoad = function (content) {
var safeName = inferTemplateName(name);
ko.templates[safeName] = content;
load(content);
},
templateName = name,
index;
//use the global configuration if it is defined
if (config !== undefined && config.loadKoTemplate !== undefined) {
if (config.loadKoTemplate.templatePath !== undefined) {
templateName = config.loadKoTemplate.templatePath + templateName;
}
if (config.loadKoTemplate.extension !== undefined) {
//if we have a ! in the path we only want the text before it
index = templateName.indexOf("!");
if (index !== -1) {
templateName = templateName.substring(0, index);
}
templateName = templateName + config.loadKoTemplate.extension;
}
}
text.load(templateName, req, onLoad, config);
},
write: function (pluginName, moduleName, write, config) {
//write the text content of the template into a module using text plugin
text.write("text", moduleName, write, config);
var safeName = inferTemplateName(moduleName);
//create a knockout template using the text content from the previous module
write.asModule(pluginName + "!" + moduleName,
"define(['text!" + moduleName + "', 'knockout', 'stringTemplateEngine'], function (content, ko) {" +
"ko.templates['" + safeName + "'] = content;" +
"});\n");
}
};
return loader;
});