Full-record display uses Handlebars template #mkwsTemplateRecor if
authorMike Taylor <mike@indexdata.com>
Tue, 26 Nov 2013 13:50:06 +0000 (13:50 +0000)
committerMike Taylor <mike@indexdata.com>
Tue, 26 Nov 2013 13:50:06 +0000 (13:50 +0000)
provided. Otherwise falls back to old hardwired behaviour.  Fixes part
of MKWS-68 ("Use a well-established and simple third-party templating
library").

tools/htdocs/mkws.js

index e560ad1..d6026c7 100644 (file)
@@ -640,6 +640,15 @@ function replaceHtml(el, html) {
 
 function renderDetails(data, marker)
 {
+    if (mkws.templateRecord === undefined) {
+       maybeLoadTemplate("Record");
+    }
+
+    if (mkws.templateRecord) {
+       var template = mkws.templateRecord;
+       return template(data);
+    }
+
     var details = '<div class="details" id="mkwsDet_'+data.recid+'"><table>';
     if (marker) details += '<tr><td colspan="2">'+ marker + '</td></tr>';
 
@@ -663,6 +672,24 @@ function renderDetails(data, marker)
     return details;
 }
 
+
+function maybeLoadTemplate(name)
+{
+    debug("trying to load template 'name'");
+    var source = $("#mkwsTemplate" + name).html();
+    debug("source = " + source);
+    if (!source) {
+       // No template: mark as not provided, fall back to hardwired behaviour
+       mkws['template' + name] = 0;
+       return;
+    }
+
+    var template = Handlebars.compile(source);
+    debug("template = " + template);
+    mkws['template' + name] = template;
+}
+
+
 function renderField(caption, data, data2, data3) {
     if (data === undefined) {
        return "";