2012-09-07 36 views
0

我在我正在構建的站點上有一個Smile timeline小部件。我已經跟隨了幾個關於如何實現和定製它的教程,迄今爲止都很好。但是我找不到使用Json服務的方式,而不是使用存儲在js文件中的全局變量中的數據。我並不擅長JavaScript,所以我找不到一種方法將整個Json輸出(相應地被格式化)存儲在一個全局變量中。微笑時間軸插件Json feed

這裏是如何在全局變量數據應該是:

var timeline_data = { 
'events' : [ 
     {'start': '2000', 
     'title': 'Barfusserkirche', 
     'description': 'by Lyonel Feininger, American/German Painter, 1871-1956', 
     'image': 'http://images.allposters.com/images/AWI/NR096_b.jpg', 
     'link': 'http://www.allposters.com/-sp/Barfusserkirche-1924-Posters_i1116895_.htm' 
     }, 

     {'start': '2001', 
     'end': '2004', 
     'title': 'Three Figures', 
     'description': 'by Kasimir Malevich, Ukrainian Painter, 1878-1935', 
     'image': 'http://images.allposters.com/images/BRGPOD/75857_b.jpg', 
     'link': 'http://www.allposters.com/-sp/Three-Figures-1913-28-Posters_i1349989_.htm' 
     }, 

     {'start': '2002',    
     'end' : '2003', 
     'title': 'Landschaft bei Montreuil', 
     'description': 'by Albert Gleizes, French Painter, 1881-1953', 
     'image': 'http://images.allposters.com/images/mer/1336_b.jpg', 
     'link': 'http://www.allposters.com/-sp/Landschaft-bei-Montreuil-Posters_i339007_.htm', 
     'isDuration' : true, 
     'icon' : "red-ico.gif",   
     'color' : '#ffcc00', 
     'textColor' : 'green'} 
]} 

,這就是它是如何通過時間軸叫:

var url = '.'; // The base url for image, icon and background image references in the data 
eventSource1.loadJSON(timeline_data, url); // The data was stored into the timeline_data variable. 

這是我做過嘗試,但我卡在如何在變量存儲整個輸出,而不僅僅是一個條目:

var timeline_data;//at global scope 
$.ajax({ 
     type: 'GET', 
     url: '/timeline/json_output.json', 
     dataType: 'json', 
     timeout: 10000, 
     crossDomain: true, 
     success: function(result) { 
      timeline_data = result; 
     } 
}); 

現在我不知道怎麼用添eline_data。認爲它是一個數組對象,不知道如何繼續下去。

在此先感謝

+0

看看這個:http://stackoverflow.com/questions/5902560/traverse-json-when-object-value-is-an-array-of-objects-with-jquery –

回答

0

我其實覺得這here,和工作就像一個魅力!

function getJson(url) { 
return JSON.parse($.ajax({ 
    type: 'GET', 
    url: url, 
    dataType: 'json', 
    global: false, 
    async:false, 
    success: function(data) { 
     return data; 
    } 
}).responseText); 
} 

var myJsonObj = getJson('myjsonurl'); 

有沒有更高效的方法?

0

試試這個:

var timeline_data;//at global scope 
$.ajax({ 
    type: 'GET', 
    url: '/timeline/json_output.json', 
    dataType: 'json', 
    timeout: 10000, 
    crossDomain: true, 
    success: function(result) { 
     for(var i = 0; i < result.events.length; ++i){ 
      $.each(result.events[i], function(key, value) { 
       console.log(key + ': ' + value); 
      }); 
     } 
    } 
});