Permalink
Browse files

Drop support for older jquery.tmpl versions (those prior to 1.0.0pre)

  • Loading branch information...
1 parent 0c1b8be commit 8076f8d3a67b6bfde0faf334af6b71e4f45ff94c @SteveSanderson SteveSanderson committed May 21, 2011
Showing with 21 additions and 54 deletions.
  1. +21 −54 src/templating/jquery.tmpl/jqueryTmplTemplateEngine.js
@@ -2,53 +2,40 @@
ko.jqueryTmplTemplateEngine = function () {
// Detect which version of jquery-tmpl you're using. Unfortunately jquery-tmpl
// doesn't expose a version number, so we have to infer it.
- this.jQueryTmplVersion = (function() {
+ // Note that as of Knockout 1.3, we only support jQuery.tmpl 1.0.0pre and later,
+ // which KO internally refers to as version "2", so older versions are no longer detected.
+ var jQueryTmplVersion = (function() {
if ((typeof(jQuery) == "undefined") || !jQuery['tmpl'])
return 0;
// Since it exposes no official version number, we use our own numbering system. To be updated as jquery-tmpl evolves.
- if (jQuery['tmpl']['tag']) {
- if (jQuery['tmpl']['tag']['tmpl'] && jQuery['tmpl']['tag']['tmpl']['open']) {
- if (jQuery['tmpl']['tag']['tmpl']['open'].toString().indexOf('__') >= 0) {
- return 3; // Since 1.0.0pre, custom tags should append markup to an array called "__"
- }
- }
- return 2; // Prior to 1.0.0pre, custom tags should append markup to an array called "_"
- }
- return 1; // Very old version doesn't have an extensible tag system
+ try {
+ if (jQuery['tmpl']['tag']['tmpl']['open'].toString().indexOf('__') >= 0) {
+ return 2; // Since 1.0.0pre, custom tags should append markup to an array called "__"
+ }
+ } catch(ex) { /* Apparently not the version we were looking for */ }
+
+ return 1; // Any older version that we don't support
})();
function ensureHasReferencedJQueryTemplates() {
- if (this.jQueryTmplVersion == 0)
- throw new Error("jquery.tmpl not detected.\nTo use KO's default template engine, reference jQuery and jquery.tmpl. See Knockout installation documentation for more details.");
+ var errorMessage = jQueryTmplVersion == 0 ? "jquery.tmpl not detected.\nTo use KO's default template engine, reference jQuery and jquery.tmpl. See Knockout installation documentation for more details."
+ : jQueryTmplVersion == 1 ? "Your version of jQuery.tmpl is too old. Please upgrade to jQuery.tmpl 1.0.0pre or later."
+ : null;
+ if (errorMessage)
+ throw new Error(errorMessage);
}
this['getTemplateNode'] = function (template) {
var templateNode = document.getElementById(template);
- if (templateNode == null)
+ if (!templateNode)
throw new Error("Cannot find template with ID=" + template);
return templateNode;
}
-
- // These two only needed for jquery-tmpl v1
- var aposMarker = "__ko_apos__";
- var aposRegex = new RegExp(aposMarker, "g");
this['renderTemplate'] = function (templateId, data, options) {
options = options || {};
- ensureHasReferencedJQueryTemplates.call(this);
-
- if (this.jQueryTmplVersion == 1) {
- // jquery.tmpl v1 doesn't like it if the template returns just text content or nothing - it only likes you to return DOM nodes.
- // To make things more flexible, we can wrap the whole template in a <script> node so that jquery.tmpl just processes it as
- // text and doesn't try to parse the output. Then, since jquery.tmpl has jQuery as a dependency anyway, we can use jQuery to
- // parse that text into a document fragment using jQuery.clean().
- var templateTextInWrapper = "<script type=\"text/html\">" + this['getTemplateNode'](templateId).text + "</script>";
- var renderedMarkupInWrapper = jQuery['tmpl'](templateTextInWrapper, data);
- var renderedMarkup = renderedMarkupInWrapper[0].text.replace(aposRegex, "'");;
- return jQuery['clean']([renderedMarkup], document);
- }
+ ensureHasReferencedJQueryTemplates();
- // It's easier with jquery.tmpl v2 and later - it handles any DOM structure
if (!(templateId in jQuery['template'])) {
// Precache a precompiled version of this template (don't want to reparse on every render)
var templateText = this['getTemplateNode'](templateId).text;
@@ -75,31 +62,11 @@ ko.jqueryTmplTemplateEngine = function () {
this['rewriteTemplate'] = function (template, rewriterCallback) {
var templateNode = this['getTemplateNode'](template);
- var rewritten = rewriterCallback(templateNode.text);
-
- if (this.jQueryTmplVersion == 1) {
- // jquery.tmpl v1 falls over if you use single-quotes, so replace these with a temporary marker for template rendering,
- // and then replace back after the template was rendered. This is slightly complicated by the fact that we must not interfere
- // with any code blocks - only replace apos characters outside code blocks.
- rewritten = ko.utils.stringTrim(rewritten);
- rewritten = rewritten.replace(/([\s\S]*?)(\${[\s\S]*?}|{{[\=a-z][\s\S]*?}}|$)/g, function(match) {
- // Called for each non-code-block followed by a code block (or end of template)
- var nonCodeSnippet = arguments[1];
- var codeSnippet = arguments[2];
- return nonCodeSnippet.replace(/\'/g, aposMarker) + codeSnippet;
- });
- }
-
- templateNode.text = rewritten;
+ templateNode.text = rewriterCallback(templateNode.text);
templateNode.isRewritten = true;
},
this['createJavaScriptEvaluatorBlock'] = function (script) {
- if (this.jQueryTmplVersion == 1)
- return "{{= " + script + "}}";
-
- // From v2, jquery-tmpl does some parameter parsing that fails on nontrivial expressions.
- // Prevent it from messing with the code by wrapping it in a further function.
return "{{ko_code ((function() { return " + script + " })()) }}";
},
@@ -108,11 +75,11 @@ ko.jqueryTmplTemplateEngine = function () {
}
ko.exportProperty(this, 'addTemplate', this.addTemplate);
- if (this.jQueryTmplVersion > 1) {
+ if (jQueryTmplVersion >= 2) {
jQuery['tmpl']['tag']['ko_code'] = {
- open: (this.jQueryTmplVersion < 3 ? "_" : "__") + ".push($1 || '');"
+ open: "__.push($1 || '');"
};
- }
+ }
};
ko.jqueryTmplTemplateEngine.prototype = new ko.templateEngine();

0 comments on commit 8076f8d

Please sign in to comment.