From 433907ae8493a49e742139612eab1ce70f1493ab Mon Sep 17 00:00:00 2001 From: Nate Lillich Date: Wed, 20 Mar 2013 20:12:24 -0700 Subject: [PATCH 1/2] Added support for functions as templates --- src/js/typeahead.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/js/typeahead.js b/src/js/typeahead.js index 335bb12d..3ffc942e 100644 --- a/src/js/typeahead.js +++ b/src/js/typeahead.js @@ -38,7 +38,7 @@ else { datasetDef.limit = datasetDef.limit || 5; - if (datasetDef.template && !datasetDef.engine) { + if (datasetDef.template && !datasetDef.engine && typeof datasetDef.template != 'function') { throw new Error('no template engine specified for ' + name); } @@ -101,7 +101,15 @@ var wrapper = '
  • %body
  • ', compiledTemplate; - if (template) { + if (typeof template == 'function') { + compiledTemplate = { + render : function(context) { + return wrapper.replace('%body', template(context)); + } + }; + } + + else if (template) { compiledTemplate = engine.compile(wrapper.replace('%body', template)); } From 626b7432eb4fccf889cde24a3c7cc133f7b09784 Mon Sep 17 00:00:00 2001 From: Jake Harding Date: Fri, 29 Mar 2013 08:35:06 -0700 Subject: [PATCH 2/2] Add test for compiled template usage. --- src/dataset.js | 2 ++ test/dataset_spec.js | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/dataset.js b/src/dataset.js index fe47ec33..31454004 100644 --- a/src/dataset.js +++ b/src/dataset.js @@ -288,10 +288,12 @@ var Dataset = (function() { wrappedTemplate, compiledTemplate; + // precompiled template if (utils.isFunction(template)) { compiledTemplate = template; } + // string template that needs to be compiled else if (utils.isString(template)) { wrappedTemplate = wrapper.replace('%body', template); compiledTemplate = engine.compile(wrappedTemplate).render; diff --git a/test/dataset_spec.js b/test/dataset_spec.js index 5b9fb1f0..59934e2f 100644 --- a/test/dataset_spec.js +++ b/test/dataset_spec.js @@ -136,6 +136,16 @@ describe('Dataset', function() { expect(this.dataset.template).toBe('boo'); }); }); + + describe('when called with a compiled template', function() { + beforeEach(function() { + this.dataset = new Dataset({ local: fixtureStrings, template: $.noop }); + }); + + it('should use it', function() { + expect(this.dataset.template).toBe($.noop); + }); + }); }); describe('#initialize', function() {