我是 Hogan.js 和 typeahead.js 的新手,所以这个问题可能很愚蠢。
$('.example-twitter-oss .typeahead').typeahead({
name: 'twitter-oss',
prefetch: 'repos.json',
template: [
'<p class="type">{{language}}</p>',
'<p class="name">{{name}}</p>',
'<p class="description">{{description}}</p>'
].join(''),
engine: Hogan
});
这就是我使用 Hogan.js 作为模板创建 typeahead.js 实例的方式。 现在我想更改我的测试数据:'repos.json',模板仍然是一样的。我猜它以某种方式缓存。如何重置我的模板以包含新数据?
我看到 hogan 有一个 compile();和 render() 方法,但我不明白如何使用它。
我使用两者的最新版本。
最佳答案
在当前版本的 typeahead.js (0.9.3) 中,数据集是通过它们的名称属性缓存的,并且没有办法 bust the cache .所以如果你有:
$('.example-twitter-oss .typeahead').typeahead({
name: 'twitter-oss',
prefetch: 'repos.json',
template: [
'<p class="type">{{language}}</p>',
'<p class="name">{{name}}</p>',
'<p class="description">{{description}}</p>'
engine: Hogan
});
$('.example-something-different .typeahead').typeahead({
name: 'twitter-oss',
prefetch: 'something_different.json'
});
.example-something-different .typeahead
不会使用指定的配置,它会使用 .example-twitter-oss .typeahead
指定的配置,因为他们使用相同的 name
。这不是直观的,它会在下一个版本 v0.10 中发生变化。不过现在,您可以通过使用不同的 name
来避免这个问题:
$('.example-twitter-oss .typeahead').typeahead({
name: 'twitter-oss',
prefetch: 'repos.json',
template: [
'<p class="type">{{language}}</p>',
'<p class="name">{{name}}</p>',
'<p class="description">{{description}}</p>'
].join(''),
engine: Hogan
});
$('.example-something-different .typeahead').typeahead({
name: 'something different',
prefetch: 'something_different.json'
});
关于javascript - 如何重置 typeahead.js 中使用的 Hogan.js 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17732640/