2015-04-29 18 views
0

我使用twitter-js-clientvis timeline來開發時間軸,我可以得到推文,我想在時間軸上顯示推文,在index.js中,我解析一個json以獲取推文我將JSON.parse()函數返回的數據傳遞給jade,我想通過使用jade在時間軸上顯示這些數據。如何訪問玉石js數組中的數據

這裏是index.js:

router.get('/data/timeline',function(req,res) { 
    twitter.getUserTimeline({screen_name:'ogedik92',count:10},error,function(data) { 
    var tweets = JSON.parse(data); 
    res.render('vistimeline',{Results:tweets}); 
    }); 

這裏是vistimeline.jade

doctype html 
html 
    head 
     title Timeline | Basic demo 

     style(type='text/css'). 
     body, html { 
      font-family: sans-serif; 
     } 

     script(src='http://visjs.org/dist/vis.js') 

     link(href='http://visjs.org/dist/vis.css', rel='stylesheet', type='text/css') 

    body 
     #visualization 

     script(type='text/javascript'). 
     // DOM element where the Timeline will be attached 
     var container = document.getElementById('visualization'); 
     // Create a DataSet (allows two way data-binding) 
     var items = new vis.DataSet([ 
      {id: 1, content: ''+Results[0].text ,start: '2015-04-20'}, 
      {id: 2, content: 'item 2', start: '2013-04-14'}, 
      {id: 3, content: 'item 3', start: '2013-04-18'}, 
      {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-05-19'}, 
      {id: 5, content: 'item 5', start: '2013-04-25'}, 
      {id: 6, content: 'item 6', start: '2013-04-27'} 
     ]); 
     // Configuration for the Timeline 
     var options = {}; 
     // Create a Timeline 
     var timeline = new vis.Timeline(container, items, options); 

我怎樣才能做到這一點?

回答

1

你可以通過這樣的在客戶端的JS變量:

doctype html 
html 
    head 
     title Timeline | Basic demo 

     style(type='text/css'). 
     body, html { 
      font-family: sans-serif; 
     } 

     script(src='http://visjs.org/dist/vis.js') 

     link(href='http://visjs.org/dist/vis.css', rel='stylesheet', type='text/css') 

    body 
     #visualization 

     script(type='text/javascript'). 
     // DOM element where the Timeline will be attached 
     var container = document.getElementById('visualization'); 
     var Results = !{JSON.stringify(Results)}; 
     // Create a DataSet (allows two way data-binding) 
     var items = new vis.DataSet([ 
      {id: 1, content: ''+Results[0].text ,start: '2015-04-20'}, 
      {id: 2, content: 'item 2', start: '2013-04-14'}, 
      {id: 3, content: 'item 3', start: '2013-04-18'}, 
      {id: 4, content: 'item 4', start: '2013-04-16', end: '2013-05-19'}, 
      {id: 5, content: 'item 5', start: '2013-04-25'}, 
      {id: 6, content: 'item 6', start: '2013-04-27'} 
     ]); 
     // Configuration for the Timeline 
     var options = {}; 
     // Create a Timeline 
     var timeline = new vis.Timeline(container, items, options); 
+0

謝謝你,解決了我的問題 –