From 2d9c5edde2e3ef2dcb50311d0fbe07e64710a270 Mon Sep 17 00:00:00 2001 From: Pststudio Date: Sun, 24 Feb 2013 19:43:55 +0100 Subject: [PATCH] Added support for compile methods in jQuery --- wrappers/jquery/mustache.js.post | 33 +++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/wrappers/jquery/mustache.js.post b/wrappers/jquery/mustache.js.post index 3209e91..d45b26a 100644 --- a/wrappers/jquery/mustache.js.post +++ b/wrappers/jquery/mustache.js.post @@ -1,13 +1,32 @@ $.mustache = function (template, view, partials) { return Mustache.render(template, view, partials); }; - - $.fn.mustache = function (view, partials) { - return $(this).map(function (i, elm) { - var template = $.trim($(elm).html()); - var output = $.mustache(template, view, partials); - return $(output).get(); - }); + var methods = { + init: function(view, partials) { + return $(this).map(function (i, elm) { + var template = $.trim($(elm).html()); + var output = $.mustache(template, view, partials); + return $(output).get(); + }); + }, + //var compiledTemplate = $('#template').mustache('compile'); + compile: function() { + var template = $.trim(this.html()); + return Mustache.compile(template); + }, + //$('#template').mustache('compilePartial', 'partial-name'); + compilePartial: function(name) { + var template = $.trim(this.html()); + Mustache.compilePartial(name, template); + return this; + } + }; + $.fn.mustache = function (method) { + if ( methods[method] ) { + return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); + } else if ( typeof method === 'object' || ! method ) { + return methods.init.apply( this, arguments ); + } }; })(jQuery);