Created
May 16, 2011 05:25
-
-
Save hunterloftis/973973 to your computer and use it in GitHub Desktop.
Zepto + Mustache + KnockoutJS starting point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ko.mustacheTemplateEngine = function () { | |
this['getTemplateNode'] = function (template) { | |
var templateNode = document.getElementById(template); | |
if (templateNode == null) | |
throw new Error("Cannot find template with ID=" + template); | |
return templateNode; | |
} | |
this['renderTemplate'] = function (templateId, data, options) { | |
options = options || {}; | |
console.log("DATA:"); | |
console.dir(data); | |
var template = this['getTemplateNode'](templateId).text, | |
html = Mustache.to_html(template, data), | |
resultNodes = Zepto(html); | |
return resultNodes; | |
}, | |
this['isTemplateRewritten'] = function (templateId) { | |
return this['getTemplateNode'](templateId).isRewritten === true; | |
}, | |
this['rewriteTemplate'] = function (template, rewriterCallback) { | |
var templateNode = this['getTemplateNode'](template); | |
var rewritten = rewriterCallback(templateNode.text); | |
templateNode.text = rewritten; | |
templateNode.isRewritten = true; | |
}, | |
this['createJavaScriptEvaluatorBlock'] = function (script) { | |
return "{{= " + script + "}}"; | |
}, | |
this.addTemplate = function (templateName, templateMarkup) { | |
document.write("<script type='text/html' id='" + templateName + "'>" + templateMarkup + "</script>"); | |
} | |
//ko.exportProperty(this, 'addTemplate', this.addTemplate); | |
}; | |
ko.mustacheTemplateEngine.prototype = new ko.templateEngine(); | |
// Use this one by default | |
ko.setTemplateEngine(new ko.mustacheTemplateEngine()); | |
//ko.exportSymbol('ko.moustacheTemplateEngine', ko.moustacheTemplateEngine); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment