From 67b091822cd530c585936d5baf78840de64c5d0d Mon Sep 17 00:00:00 2001 From: Paulius Date: Wed, 4 Jun 2014 20:07:26 +0300 Subject: [PATCH 1/2] Added support for partials object. --- jquery.mustache.js | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/jquery.mustache.js b/jquery.mustache.js index 4b679fc..e52b282 100644 --- a/jquery.mustache.js +++ b/jquery.mustache.js @@ -112,19 +112,44 @@ templateMap = {}; getMustache().clearCache(); } + + /** + * Gets partials object (i.e. map {partial name -> actual template content}) + * as required by Mustache. + */ + function getPartialsObject(partials) { + var partialsObject = $.extend({}, templateMap); + + if (partials !== void 0) { + $.each(partials, function(partialName, templateName) { + if (has(templateName)) { + partialsObject[partialName] = templateMap[templateName]; + } + else { + if (options.warnOnMissingTemplates) { + $.error('No template registered for: ' + templateName); + } + partialsObject[partialName] = ''; + } + }); + } + + return partialsObject; + } /** * Renders a previously added Mustache template using the supplied templateData object. Note if the supplied * templateName doesn't exist an empty String will be returned. */ - function render(templateName, templateData) { + function render(templateName, templateData, partials) { if (!has(templateName)) { if (options.warnOnMissingTemplates) { $.error('No template registered for: ' + templateName); } return ''; } - return getMustache().to_html(templateMap[templateName], templateData, templateMap); + + return getMustache().to_html(templateMap[templateName], templateData, getPartialsObject(partials)); } /** @@ -184,14 +209,15 @@ * @param templateData One or more JavaScript objects which will be used to render the Mustache * template. * @param options.method jQuery method to use when rendering, defaults to 'append'. + * @param partials Optional dynamic partials map which will be used to render the template. */ - $.fn.mustache = function (templateName, templateData, options) { + $.fn.mustache = function (templateName, templateData, options, partials) { var settings = $.extend({ method: 'append' }, options); var renderTemplate = function (obj, viewModel) { - $(obj)[settings.method](render(templateName, viewModel)); + $(obj)[settings.method](render(templateName, viewModel, partials)); }; return this.each(function () { From be17b917048a1a107fa0953e051bfe7d6bd6d0e6 Mon Sep 17 00:00:00 2001 From: Paulius Date: Wed, 4 Jun 2014 20:18:08 +0300 Subject: [PATCH 2/2] Fixed indentation. --- jquery.mustache.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jquery.mustache.js b/jquery.mustache.js index e52b282..a174904 100644 --- a/jquery.mustache.js +++ b/jquery.mustache.js @@ -120,7 +120,7 @@ function getPartialsObject(partials) { var partialsObject = $.extend({}, templateMap); - if (partials !== void 0) { + if (partials !== void 0) { $.each(partials, function(partialName, templateName) { if (has(templateName)) { partialsObject[partialName] = templateMap[templateName]; @@ -133,7 +133,7 @@ } }); } - + return partialsObject; }