Last active
December 14, 2015 01:09
-
-
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
This file contains hidden or 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
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