Skip to content

Instantly share code, notes, and snippets.

@kamranayub
Created March 21, 2012 19:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamranayub/2151977 to your computer and use it in GitHub Desktop.
Save kamranayub/2151977 to your computer and use it in GitHub Desktop.
Pre-compiled jQuery tmpl support for KO 2.0.0
// jQuery.tmpl Compiled Source Plugin for Knockout 2.0
// Kamran Ayub - http://kamranicus.com
//
// Adds support for referencing named pre-compiled templates
// e.g. $.template('name', 'markup')
//
// Specifically, this makes Cassette Knockout compiled templates
// work in KO 2.0.0
(function (ko) {
ko.templateSources.compiledTemplateSource = function (name) {
this._data = {};
this._data["precompiled"] = name;
};
ko.templateSources.compiledTemplateSource.prototype['text'] = function (/* valueToWrite */) {
return this._data["precompiled"];
};
ko.templateSources.compiledTemplateSource.prototype['data'] = function (key /*, valueToWrite */) {
if (arguments.length === 1) {
return this._data[key];
} else {
this._data[key] = arguments[1];
}
};
var templateEngine = new ko.jqueryTmplTemplateEngine(),
__super_makeTemplateSource = templateEngine['makeTemplateSource'];
templateEngine['makeTemplateSource'] = function (template) {
if (jQuery['template'][template]) {
return new ko.templateSources.compiledTemplateSource(template);
} else {
return __super_makeTemplateSource.call(this, template);
}
};
ko.setTemplateEngine(templateEngine);
})(ko);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment