Created
March 21, 2012 19:47
-
-
Save kamranayub/2151977 to your computer and use it in GitHub Desktop.
Pre-compiled jQuery tmpl support for KO 2.0.0
This file contains 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
// 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