【发布时间】: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