简体   繁体   English

带有Ember的HTMLBars的Typeahead.js

[英]Typeahead.js with Ember's HTMLBars

We've been using the Typeahead.js library in our Ember app (via this addon ) with success on Ember versions prior to 1.10, but the upgrade to Ember 1.10 is causing us problems. 我们一直在Ember应用程序中通过该插件使用Typeahead.js库(通过此插件 ),在1.10之前的Ember版本上取得了成功,但是升级到Ember 1.10却给我们带来了问题。

Until now we've had success compiling templates that are passed into the typeahead component and passing that along to the typeahead library like this: 到目前为止,我们已经成功地编译了传递到typeahead组件中的模板,并将其传递给typeahead库,如下所示:

templates: {
  // this.get('suggestionTemplate') is a string of handlebars template
  suggestion: Handlebars.compile(this.get('suggestionTemplate')),
  <other code>
}

This does not work in Ember 1.10, however, as typeahead.js throws the below error when executing this line of code: 但是,这在Ember 1.10中不起作用,因为执行此行代码时typeahead.js抛出以下错误:

Code: 码:

$el = $(html.suggestion).append(that.templates.suggestion(suggestion)).data(datasetKey, that.name).data(valueKey, that.displayFn(suggestion)).data(datumKey, suggestion);

Error: 错误:

Uncaught TypeError: that.templates.suggestion is not a function

Previously that.templates.suggestion , which is the value from the first code block above, was a function that could be passed the object suggestion and it would compile the actual template. 以前that.templates.suggestion是上面第一个代码块中的值,它是一个可以传递给对象suggestion的函数,它将编译实际的模板。 With HTMLBars, that.templates.suggestion is no longer a function but rather is an HTMLBars object, so this code no longer works. 使用HTMLBars, that.templates.suggestion不再是函数,而是HTMLBars对象,因此该代码不再起作用。

Is there a better way to do this same thing in Ember 1.10 that will match the previous behavior? 在Ember 1.10中是否有更好的方法可以执行此操作以匹配以前的行为?

I remember I had a similar problem using Handlebars.compile I ended up opting for passing a function to suggestion instead, and then the template content: 我记得我在使用Handlebars.compile遇到了类似的问题,最终选择了将函数传递给suggestion ,然后传递模板内容:

templates: {
  empty: '<span>No results</span>',
  suggestion: function(item){
   return '<div>' + item.name + '</div>';
  }
}

Hope this works for you too. 希望这对您也有用。

如果不再需要维护此类附加组件,建议您直接使用typeahead(无cli附加组件),自Ember 1.7起我就一直在使用它,而现在我使用的是1.11

最后,我们只是在bower.json保留了一个单独的handlebars依赖项,因为看来bower.json库要求您将bower.json handlebars模板传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM