2014-03-13 56 views
0

我想抓住從谷歌日曆的一些JSON數據,並將其送入所謂的「簡單活動日曆」一個jQuery插件數組麻煩將谷歌日曆JSON飼料轉化爲簡單的日曆輸入

插件希望的東西像這樣:

var events_array = new Array(
{ 
    startDate: new Date(2014,02,12,17,30), 
    endDate: new Date(2014,02,12,18,0), 
    title: 'eventName', 
    description: 'eventSummary', 
    priority: 3, // 1 = Low, 2 = Medium, 3 = Urgent 
    frequency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly 
}, 
{ 
    startDate: new Date(2014,02,10,20,50), 
    endDate: new Date(2014,02,10,20,59), 
    title: "Event 2", 
    description: "Description 2", 
    priority: 1, 
    frequency:1 
}, 
{ 
    startDate: new Date(2014,07,20,18,0), 
    endDate: new Date(2014,00,20,21,30), 
    title: "Event 3", 
    description: "Description 3", 
    priority: 3, 
    frequency:1 
} 
); 

我抓住和解析這樣的數據:

var urllist = ["http://www.google.com/calendar/feeds/events%40wilsonabbey.com/public/full?  orderby=starttime&sortorder=ascending&max-results=3&futureevents=true&alt=json"]; 
var JSONData = { count: 0, 
    value : { 
     description: "Wilson Abbey Events", 
     generator: "StackOverflow communal coding", 
     items: [] 
}}; 

urllist.forEach(function addFeed(url){ 
    $.getJSON(url, complete); 
}); 

function complete(result, status, jqXHR) { 
    var eventName = []; 
    var eventSummary = []; 
    var data_element = ''; 
     var events_array = new Array(); 
    // Track the number of calls completed back, we're not done until all 3 
    // asynchronous calls have returned 
    if(typeof complete.count === 'undefined') 
     complete.count = urllist.length; 

    if(!result.feed.entry) { 
     console.log("No entries from "); 
    } else { 
     JSONData.count += result.feed.entry.length; 
    JSONData.value.items = JSONData.value.items.concat(result.feed.entry); 
} 
//console.log(complete.count); 
if (!(--complete.count)) { 
    console.log(JSONData); 
    var str = ""; 
    for (var i = 0; i < JSONData.value.items.length; i++) { 
     var item = JSONData.value.items[i]; 
     str += item.author[0].name.$t + " (starts " + item.gd$when[0].startTime + "): " + item.title.$t + "\n"; 
     eventName[i] = item.title.$t; 
     eventSummary[i] = item.content.$t; 
     dataItem = 'startDate: new Date(2014,02,12,17,30),'; 
     dataItem += 'endDate: new Date(2014,02,12,18,0),'; 
     dataItem += 'title: ' + eventName[i] + ','; 
     dataItem += 'description: ' + eventSummary[i] + ','; 
     dataItem += 'priority: 3,'; 
     dataItem += 'frequency: 1'; 
     events_array[i] = dataItem; 
     } 
     console.log(events_array); 
     $("#calendar").dp_calendar({ 
      events_array: events_array 
     }); 

     } 
} 

大部分代碼是很有幫助社區提供在這裏,最我的問題是我正在將數據放入簡單事件日曆將接受的格式。

感謝您的任何建議!

裏克·米爾斯

但我得到的錯誤(從控制檯) 遺漏的類型錯誤:未定義

回答

0

想通了,無法調用方法「調用getHours」 - 我需要創建對象的數組和我創建一個字符串文字數組。

新的代碼替換for循環:

var obj = { 
startDate: new Date(2014,02,14,17,30), 
endDate: new Date(2014,02,14,18,0), 
title: item.title.$t, 
description: 'eventSummary', 
priority: 3, // 1 = Low, 2 = Medium, 3 = Urgent 
frequency: 1 // 1 = Daily, 2 = Weekly, 3 = Monthly, 4 = Yearly 
}; 
events_array.push(obj);