【问题标题】:How to specify precompiled template for typeahead when it is used with ember js?与ember js一起使用时如何为typeahead指定预编译模板?
【发布时间】:2014-04-14 05:18:02
【问题描述】:

我正在使用 typeahead js 和 Ember。当我初始化 typeahead 时,我可以使用 Handlebars.compile 指定一个自定义模板,但为此我必须包含完整版本的把手,而不仅仅是运行时。这显然不是一个理想的结果。 如果我尝试在下面的代码中包含预编译的模板,我会得到 ​​p>

未捕获的类型错误:无法调用未定义的方法“push”

错误。 这是我的代码-

$("input[name='myTypeAhead']").typeahead("destroy").typeahead({
        hint: true,
        highlight: true
  },
  {
        name: "prerequisites",
        displayKey: "val",
        source: this.bhObject.ttAdapter(),
        templates: {
              suggestion: Ember.TEMPLATES["suggestions"]
              // suggestion: Handlebars.compile("<p><strong>{{val}}</strong></p>")
        }

   });

我的建议模板有这个 -

<p><strong>{{val}}</strong></p>

整个调用栈如下-

Uncaught TypeError: Cannot call method 'push' of undefined suggestions.js:9
anonymous suggestions.js:9
(anonymous function) handlebars.js:436
getSuggestionNode typeahead.bundle.js:1116
jQuery.extend.map jquery.js:686
getSuggestionsHtml typeahead.bundle.js:1107
render typeahead.bundle.js:1095
renderIfQueryIsSame typeahead.bundle.js:1146
get typeahead.bundle.js:619
proxy jquery.js:731
update typeahead.bundle.js:1144
updateDataset typeahead.bundle.js:1314
reverseArgs typeahead.bundle.js:34
jQuery.extend.each jquery.js:590
_.each typeahead.bundle.js:32
update typeahead.bundle.js:1312
onQueryChanged typeahead.bundle.js:1461
flush typeahead.bundle.js:792
trigger typeahead.bundle.js:783
checkInputValue typeahead.bundle.js:968
onInput typeahead.bundle.js:929
proxy jquery.js:731
jQuery.event.dispatch jquery.js:4676
elemData.handle jquery.js:4361

有人可以建议解决这个问题的正确方法是什么吗? 谢谢。

【问题讨论】:

  • 您可能在车把运行时之前加载模板文件...
  • @CodeJack 如果他这样做了,一旦 Ember 模板尝试调用 Ember.Handelbars.template(),就会抛出错误。

标签: javascript ember.js handlebars.js typeahead.js


【解决方案1】:

我对此不是 100% 的,但我认为这是因为 Ember 对 Handlebars 所做的更改。 Ember 为 Handlebars 添加了功能,使其更适合 Ember 模板。因此,正常的 Handlebars 输出与 Ember Handlebars 输出不同。我的建议是不要使用 Ember 预编译器,而只是使用普通的 Handlebars 预编译器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 2014-07-01
    • 2012-01-29
    • 2023-04-07
    • 2017-09-26
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多