-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkoObservableTemplateEngine-amd.js
126 lines (107 loc) · 4.65 KB
/
koObservableTemplateEngine-amd.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Knockout Observable Template Engine
// Author: Giles Bradshaw
// License: MIT (http://www.opensource.org/licenses/mit-license)
// Version 0.0.1
// unashamably based on https://github.com/ifandelse/Knockout.js-External-Template-Engine/blob/master/src/ExternalTemplateSource.js
// This is the AMD style version of the KO Observable Template Engine
// If you need the 'standard js lib' module version, please go to https://github.com/gilesbradshaw/Knockout.js-Observable-Template-Engine
define(['knockout', 'jquery'], function (ko, jQuery) {
var ObservableTemplateSource = function (templateId, options) {
var self = this, origAfterRender;
self.templateId = templateId;
self.loaded = false;
self.template = ko.observable();
self.template.data = {};
self.options = ko.utils.extend({}, options);
self.options.templateId = templateId;
if (self.options && self.options.afterRender) {
origAfterRender = self.options.afterRender;
self.options.afterRender = function () {
if (self.loaded) {
origAfterRender.apply(self.options, arguments);
}
}
}
};
ko.utils.extend(ObservableTemplateSource.prototype, {
data: function (key, value) {
if (arguments.length === 1) {
if (key === "precompiled") {
this.template();
}
return this.template.data[key];
}
this.template.data[key] = value;
},
text: function (value) {
if (!this.loaded) {
if (this.options.template) {
this.template(ko.utils.unwrapObservable(this.options.template));
if (ko.isObservable(this.options.template)) {
var _this = this;
this.options.template.subscribe(function (data) { _this.template(data); });
}
}
this.loaded = true;
}
if (arguments.length === 0) {
return this.template();
} else {
this.template(arguments[0]);
}
}
});
var KoObservableTemplateEngine = function (koEngineType) {
var engine = koEngineType ? new koEngineType() : new ko.nativeTemplateEngine();
engine.cachedTemplates ={};
var cacheId = 0;
engine.makeTemplateSource = function (template, bindingContext, options) {
// Named template
if (typeof template == "string") {
if(!options.template)
{
var elem = document.getElementById(template);
return new ko.templateSources.domElement(elem);
}
else {
//no cache means don't cache the template by the name
if (!template.cacheId) {
template.cacheId = "cache" + cacheId++;
engine.cachedTemplates[template.cacheId] = new ObservableTemplateSource(template, options);
}
return engine.cachedTemplates[template.cacheId];
}
}
else if ((template.nodeType == 1) || (template.nodeType == 8)) {
// Anonymous template
return new ko.templateSources.anonymousTemplate(template);
}
};
engine.renderTemplate = function (template, bindingContext, options) {
var templateSource = engine.makeTemplateSource(template, bindingContext, options);
if (!options.svg)
return engine.renderTemplateSource(templateSource, bindingContext, options);
else {
var doc = new window.DOMParser().parseFromString(
'<svg xmlns="http://www.w3.org/2000/svg">' + templateSource.text() + '</svg>',
'application/xml');
doc = document.importNode(doc.documentElement, true);
var ret = [];
var child = doc.firstChild;
while (child) {
ret.push(child);
child = child.nextSibling;
}
return ret;
}
};
return engine;
};
ko.KoObservableTemplateEngine = KoObservableTemplateEngine;
if (jQuery['tmpl'] && jQuery['tmpl']['tag']['tmpl']['open'].toString().indexOf('__') >= 0) {
ko.setTemplateEngine(new KoObservableTemplateEngine(ko.jqueryTmplTemplateEngine));
}
else {
ko.setTemplateEngine(new KoObservableTemplateEngine());
}
});