【发布时间】:2014-02-03 19:26:40
【问题描述】:
我正在使用这个插件http://almende.github.com/chap-links-library/js/timeline/doc/ 从一些 JSON 数据中呈现时间线。以下是完整的相关代码:
<script>
function doEverything() {
var timeline;
$.getJSON("tljson.json",function(result){
items = [JSON.stringify(result)];
drawVisualization();
});
function drawVisualization() {
var options = {'width': '100%'};
timeline = new links.Timeline(document.getElementById('mytimeline'));
timeline.draw(window.items, options);
};
}
</script>
<body onload="doEverything();">
当我提醒“window.items”时;它按预期显示。但是,检查器上的控制台显示
Uncaught TypeError: Cannot call method 'valueOf' of undefined
而且时间线没有渲染。我做错了什么?
编辑:
console.log(JSON.stringify(result)) 的输出:
[{"content":"rubygem-aws-2.6.0 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=863368'>View on BugZilla</a>","start":"863368"},{"content":"Recent update of rubygem-nokogiri breaks rubygem-aws-sdk<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=816706'>View on BugZilla</a>","start":"816706"},{"content":"Please update rubygem-aws-sdk to 1.3.5<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=800616'>View on BugZilla</a>","start":"800616"},{"content":"Review Request: rubygem-aws-sdk - AWS SDK for Ruby<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=723472'>View on BugZilla</a>","start":"723472"},{"content":"Please add EPEL branches for rubygem-aws<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=723152'>View on BugZilla</a>","start":"723152"},{"content":"Unable to resolve dependency of rubygem-aws-2.4.2.2-2.fc15 (updates)<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=716343'>View on BugZilla</a>","start":"716343"},{"content":"rubygem-aws-2.5.7 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=703433'>View on BugZilla</a>","start":"703433"},{"content":"rubygem-aws-2.5.1 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=703117'>View on BugZilla</a>","start":"703117"},{"content":"rubygem-aws-2.4.5 is available<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=691728'>View on BugZilla</a>","start":"691728"},{"content":"Update rubygem-aws to 2.3.34<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=668955'>View on BugZilla</a>","start":"668955"},{"content":"Review Request: rubygem-aws - Ruby gem for all Amazon Web Services<br><a href='https://bugzilla.redhat.com/show_bug.cgi?id=637378'>View on BugZilla</a>","start":"637378"}]
编辑:最新代码:(控制台仍为timeline.draw返回未知数据类型):
<script>
function doEverything() {
var timeline;
$.getJSON("tljson.json",function(result){
drawVisualization(result);
});
function drawVisualization(result) {
//console.log (JSON.stringify(result));
var options = {
'width': '100%'
};
timeline = new links.Timeline(document.getElementById('mytimeline'));
timeline.draw(JSON.stringify(result), options);
};
}
</script>
<body onload="doEverything();">
【问题讨论】:
-
在 Chrome 中设置“中断所有异常”并查看实际原因。
-
items不作为全局变量存在,直到执行为其赋值的代码行。在那之前,它是不存在的。这就是为什么您应该在适当的范围内声明全局(和所有其他)变量。 -
我不认为这是问题所在,因为当我使用
alert (window.items)时,它会显示 JSON 数据的正确值。 -
这是你的全部代码吗? bcoz 我不认为问题是窗口对象?
标签: javascript json chap-links-library