<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>
<div><input type="text" name="typeahead-example" placeholder="Type here" class="typeahead-example"></div>
<script src="./js/jquery-1.10.2.js" type="text/javascript"></script>
<script src="./js/hogan.js" type="text/javascript"></script>
<script src="./js/typeahead.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('input.typeahead-example').typeahead({
name: 'example',
local: [{value: 'Abc Def', tokens: ['Abc', 'Def'], name: 'random1', val2: 'A', val3: 'B'},
{value: 'Def Ghi', tokens: ['Def', 'Ghi'], name: 'random2', val2: 'C', val3: 'D'},
{value: 'Ghi Jkl', tokens: ['Ghi', 'Jkl'], name: 'random3', val2: 'E', val3: 'F'},
{value: 'Jkl Mno', tokens: ['Jkl', 'Mno'], name: 'random4', val2: 'G', val3: 'H'}],
limit: 3,
valueKey: 'name',
template: '<p>{{value}}</p>',
engine: Hogan
});
});
</script>
</body>
</html>
这是我使用prefetch优化后的代码版本:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo</title>
</head>
<body>
<div><input type="text" name="typeahead-example" placeholder="Type here" class="typeahead-example"></div>
<script src="./js/jquery-1.10.2.js" type="text/javascript"></script>
<script src="./js/hogan.js" type="text/javascript"></script>
<script src="./js/typeahead.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('input.typeahead-example').typeahead({
name: 'example',
prefetch: {url: './json/example.json', ttl: '0'},
limit: 3,
valueKey: 'name',
template: '<p>{{value}}</p>',
engine: Hogan
});
});
</script>
</body>
</html>
这是一个example.json文件的例子:
[{value: 'Abc Def', tokens: ['Abc', 'Def'], name: 'random1', val2: 'A', val3: 'B'}, {value: 'Def Ghi', tokens: ['Def', 'Ghi'], name: 'random2', val2: 'C', val3: 'D'}, {value: 'Ghi Jkl', tokens: ['Ghi', 'Jkl'], name: 'random3', val2: 'E', val3: 'F'}, {value: 'Jkl Mno', tokens: ['Jkl', 'Mno'], name: 'random4', val2: 'G', val3: 'H'}]
问题出现在Chrome和Firefox中。在Chrome中进行调试,我可以看到example.json文件已被服务并接收。例如.json的网络活动预览和响应显示了文件的全部内容。然而,在Chrome调试器的资源部分,本地存储为空。没有控制台错误,只有这个消息。
XHR finished loading: "http://localhost:8000/json/example.json". jquery-1.10.2.js:8706
send jquery-1.10.2.js:8706
jQuery.extend.ajax jquery-1.10.2.js:8136
jQuery.(anonymous function) jquery-1.10.2.js:8282
jQuery.extend.getJSON jquery-1.10.2.js:8265
c.mixin._loadPrefetchData typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin.initialize typeahead.min.js:7
proxy jquery-1.10.2.js:827
(anonymous function) typeahead.min.js:7
jQuery.extend.map jquery-1.10.2.js:782
g typeahead.min.js:7
jQuery.extend.each jquery-1.10.2.js:657
jQuery.fn.jQuery.each jquery-1.10.2.js:266
b.initialize typeahead.min.js:7
jQuery.fn.typeahead typeahead.min.js:7
(anonymous function) prefetch.html:14
fire jquery-1.10.2.js:3048
self.fireWith jquery-1.10.2.js:3160
jQuery.extend.ready jquery-1.10.2.js:433
completed
关于这篇帖子,我没有跨域问题;关于这篇帖子,我将ttl设置为0,但问题仍然存在。希望得到帮助。
- 更新* 根据@NitzanShaked的建议,我添加了一些控制台记录。
首先,我尝试了这个方法,但控制台没有记录任何内容。
$(document).ready(function(){
$('input.typeahead-example').typeahead({
name: 'example',
prefetch: {
url: './json/example.json',
ttl: '0',
filter: function (data) {
console.log(data);
for (var i = 0; i < data.length; i++) {
datum = data[i];
console.log(datum);
}
return data;
}
},
limit: 3,
valueKey: 'name',
template: '<p>{{value}}</p>',
engine: Hogan,
});
});
然后我尝试了这个:
$(document).ready(function(){
$('input.typeahead-example').typeahead({
name: 'example',
prefetch: {url: './json/example.json', ttl: '0'},
limit: 3,
valueKey: 'name',
template: '<p>{{value}}</p>',
engine: Hogan,
}).bind('typeahead:opened', function (obj, datum) {
console.log(obj);
console.log(datum);
console.log(datum.val2);
});
});
当我点击输入框时,控制台记录了以下内容。
jQuery.Event {type: "typeahead:opened", timeStamp: 1378502920480, jQuery1102039872112357988954: true, isTrigger: 3, namespace: ""…}
currentTarget: input.typeahead-example tt-query
data: null
delegateTarget: input.typeahead-example tt-query
handleObj: Object
isTrigger: 3
jQuery1102039872112357988954: true
namespace: ""
namespace_re: null
result: undefined
target: input.typeahead-example tt-query
timeStamp: 1378502920480
type: "typeahead:opened"
__proto__: Object
prefetch.html:22
undefined prefetch.html:23
Uncaught TypeError: Cannot read property 'val2' of undefined prefetch.html:24
(anonymous function) prefetch.html:24
jQuery.event.dispatch jquery-1.10.2.js:5095
elemData.handle jquery-1.10.2.js:4766
jQuery.event.trigger jquery-1.10.2.js:5007
(anonymous function) jquery-1.10.2.js:5691
jQuery.extend.each jquery-1.10.2.js:657
jQuery.fn.jQuery.each jquery-1.10.2.js:266
jQuery.fn.extend.trigger jquery-1.10.2.js:5690
c.mixin.trigger typeahead.min.js:7
c.mixin._propagateEvent typeahead.min.js:7
proxy jquery-1.10.2.js:827
d.trigger typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin.open typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin._openDropdown typeahead.min.js:7
proxy jquery-1.10.2.js:827
d.trigger typeahead.min.js:7
proxy jquery-1.10.2.js:827
c.mixin._handleFocus typeahead.min.js:7
proxy jquery-1.10.2.js:827
jQuery.event.dispatch jquery-1.10.2.js:5095
elemData.handle
name
(在您的情况下为"example"
),以确保它不是从本地存储中获取的。 - Nitzan Shakedfilter
,然后使用console.log
打印参数。确保它有意义。如果没问题,试着从filter
返回一个虚拟的Datum,以确保它正常工作。 - Nitzan Shaked