让typeahead.js使用动态内容,Bottle和Hogan

时间:2013-12-18 07:23:14

标签: jquery bottle typeahead.js hogan.js

预期行为

搜索字段应显示类似“Twitter上的开源项目”示例的结果:

http://twitter.github.io/typeahead.js/examples/

当前行为

  • 输入搜索文本时,没有显示下拉列表。
  • Firebug中没有错误。
  • 所有必要的'容器'div似乎都在搜索字段下方加载(但隐藏了display:none)。
  • 不会生成或显示结果。
  • Firebug > Net在加载内容时显示对json文件的GET请求,并且内容在响应选项卡中可用。

疑难解答

动态内容

网站内容通过getJSON()动态加载。

Hogan和先行脚本包含在头部

<script src="https://path/to/typeahead.min.js"></script>
<script src="https://path/to/hogan-2.0.0.js"></script>

Bottle and Hogan

我使用Bottle,它也使用Hogan使用的默认占位符,即{{value here}}

所以在Hogan.js中,我改变了默认的分界符:

otag = '{{',
ctag = '}}';

为:

otag = '[[',
ctag = ']]';

功能

我创建了一个在加载动态内容后运行的函数:

<script>
function getTypeAheadReady() {
$('.example-twitter-oss .typeahead').typeahead({                              
name: 'mygreatsearch',
valueKey: 'my_field_one',  
prefetch: 'https://path/to/static/courses.json',
template: [
'<p class="my-field-1">[[my_field_one]]</p>',
'<p class="my-field-2">[[my_field_two]]</p>',
'<p class="my-field-3">[[my_field_three]]</p>'
].join(''),
engine: Hogan  
})
}
</script>

拨打

$("#content").html("");
$("#content").append(results.content);
getTypeAheadReady(); // re-initialise typeahead.js functionality for search

JSON

我的JSON文件似乎是正确的:

[
{ "my_field_one": "val1", "my_field_two": "val2", "my_field_three": "val3". "tokens":["something"] },
{ "my_field_one": "val4", "my_field_two": "val5", "my_field_three": "val6", "tokens":["somethingelse"] },
]

它位于https://path/to/static/courses.json,我可以直接在浏览器中访问它。

HTML (从数据库加载)

<div class="example example-twitter-oss">
<div class="demo">
<input class="typeahead" type="text" placeholder="Placeholder 'text'">
</div>
</div>

CSS

CSS似乎是正确的:

.tt-dropdown-menu {
  text-align: left;
}

.title,
.my-field-1 {
  font-family: Prociono;
}

.title {
  margin: 20px 0 0 0;
  font-size: 64px;
}

.example {
  padding: 30px 0;
}

.course-title {
  margin: 20px 0;
  font-size: 32px;
}

.demo {
  position: relative;
  *z-index: 1;
  margin: 50px 0;
}

.typeahead,
.tt-query,
.tt-hint {
  width: 396px;
  height: 30px;
  padding: 8px 12px;
  font-size: 24px;
  line-height: 30px;
  border: 2px solid #ccc;
  -webkit-border-radius: 8px;
     -moz-border-radius: 8px;
      border-radius: 8px;
  outline: none;
}

.typeahead {
  background-color: #fff;
}

.typeahead:focus {
  border: 2px solid #0097cf;
}

.tt-query {
  -webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
     -moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
      box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}

.tt-hint {
  color: #999
}

.tt-dropdown-menu {
  width: 422px;
  margin-top: 12px;
  padding: 8px 0;
  background-color: #fff;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, 0.2);
  -webkit-border-radius: 8px;
     -moz-border-radius: 8px;
      border-radius: 8px;
  -webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
     -moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
      box-shadow: 0 5px 10px rgba(0,0,0,.2);
}

.tt-suggestion {
  padding: 3px 20px;
  font-size: 18px;
  line-height: 24px;
}

.tt-suggestion.tt-is-under-cursor {
  color: #fff;
  background-color: #0097cf;

}

.tt-suggestion p {
  margin: 0;
}

.example-twitter-oss .tt-suggestion {
  padding: 8px 20px;
}

.example-twitter-oss .tt-suggestion + .tt-suggestion {
  border-top: 1px solid #ccc;
}

.example-twitter-oss .my-field-2 {
  float: right;
  font-style: italic;
}

.example-twitter-oss .my-field-1 {
  font-weight: bold;
}

.example-twitter-oss .my-field-3 {
  font-size: 14px;
}

1 个答案:

答案 0 :(得分:1)

<强>解决方案:

.json文件中第二个对象后的逗号逗号。