jquery – 前置自定义模板没有Handlebars

前端之家收集整理的这篇文章主要介绍了jquery – 前置自定义模板没有Handlebars前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图实现Typeahead.JS “Custom Template” example
  1. $('#custom-templates .typeahead').typeahead(null,{
  2. name: 'best-pictures',displayKey: 'value',source: bestPictures.ttAdapter(),templates: {
  3. empty: [
  4. '<div class="empty-message">','unable to find any Best Picture winners that match the current query','</div>'
  5. ].join('\n'),suggestion: Handlebars.compile('<p><strong>{{value}}</strong> – {{year}}</p>')
  6. }
  7. });

具体如下:

  1. suggestion: Handlebars.compile('<p><strong>{{value}}</strong> – {{year}}</p>')

最初我没有意识到你需要明确地要求Handlebars作为依赖:

  1. Uncaught ReferenceError: Handlebars is not defined

当我删除Handlebars …

  1. suggestion: '<p><strong>' + value + '</strong> – ' + year + '</p>'

它给出另一个JS错误

  1. Uncaught ReferenceError: value is not defined

是否可以使用自定义视图模板,而不使用Handlebars引擎?

解决方法

使用此格式:
  1. suggestion: function(data) {
  2. return '<p><strong>' + data.value + '</strong> – ' + data.year + '</p>';
  3. }

取自this thread

猜你在找的jQuery相关文章