Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active December 14, 2015 01:09
Show Gist options
  • Save miniplay/5003763 to your computer and use it in GitHub Desktop.
Save miniplay/5003763 to your computer and use it in GitHub Desktop.
Example of custom templates and suggestion properties for https://github.com/twitter/typeahead.js
function MiniTemplateEngine() {
this.compile = function(templateContents) {
var parentTemplate = '<li class="tt-suggestion">%body</li>';
switch(templateContents) {
case parentTemplate.replace("%body", "test"):
return new MiniTemplateSample();
break;
default:
return new MiniTemplateSample();
break;
}
};
}
function MiniTemplateSample() {
/**
* @param templateData In this case, a typeahead Datum object
*/
this.render = function(templateData) {
return "<li class='tt-suggestion' data-uid='" + templateData.uid + "'>Mini-" +
templateData.value + "</li>";
}
}
var testEngine = new MiniTemplateEngine();
$('#testInputAutofill').typeahead([
{
name: 'games1',
/* Sample datasources. If you specify items as objects must specify at minimum
* "value" and "tokens" properties.
* Additional properties can be added, and will be available for the template render method */
local: [
{ value:"Doom", tokens:["doom"], uid:"G1-1" },
{ value:"Doom 3", tokens:["doom", "3"], uid:"G1-2" }
],
template: "test",
engine: testEngine
},
{
name: 'games2',
local: [
{ value:"Alpha Centauri", tokens:["alpha", "centauri"], uid:"G2-1" },
{ value:"Alien Crossfire", tokens: ["alien", "crossfire"], uid:"G2-2" }
],
template: "test",
engine: testEngine
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment