Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示来自多个Google日历的所有事件_Javascript_Google Calendar Api - Fatal编程技术网

Javascript 显示来自多个Google日历的所有事件

Javascript 显示来自多个Google日历的所有事件,javascript,google-calendar-api,Javascript,Google Calendar Api,我有多个谷歌日历,我想使用javascript上的谷歌日历api将它们合并显示在一个列表中(如谷歌日历ui上) 天气很好。主要的剪报如下: function listUpcomingEvents() { gapi.client.calendar.events.list({ 'calendarId': 'primary', 'timeMin': (new Date()).toISOString(), 'showDeleted': false,

我有多个谷歌日历,我想使用javascript上的谷歌日历api将它们合并显示在一个列表中(如谷歌日历ui上)

天气很好。主要的剪报如下:

function listUpcomingEvents() {
    gapi.client.calendar.events.list({
      'calendarId': 'primary',
      'timeMin': (new Date()).toISOString(),
      'showDeleted': false,
      'singleEvents': true,
      'maxResults': 10,
      'orderBy': 'startTime'
    }).then(function(response) {
      var events = response.result.items;
      appendPre('Upcoming events:');

      if (events.length > 0) {
        for (i = 0; i < events.length; i++) {
          var event = events[i];
          var when = event.start.dateTime;
          if (!when) {
            when = event.start.date;
          }
          appendPre(event.summary + ' (' + when + ')')
        }
      } else {
        appendPre('No upcoming events found.');
      }
    });
  }
由于提到的calendarId:“primary”只接受一个值(而不是列表),我必须多次调用端点gapi.client.calendar.events.list以获取所有事件

但当我想让我的事件有序(使用开始日期,因为它是日历)时,我必须对它们进行排序


因为它是异步的,我不知道如何收集所有事件,然后对它们进行排序。有什么办法吗?(或者同时从多个日历请求所有活动/通话?

我想到的一件事是你可以使用

一个可能的解决办法是:

function callAllCalendars() {
   var promises = []
   var calendarIdList = ["calendar1", "calendar2", "calendar3"];
   for(var i = 0; i < calendarIdList .length, i++){
      promises.push(createCalendarPromise(calendarIdList [i]));
   }
   $.when.apply($, promises).done(function () {
      ... Your code to do the sorting ...
   });

}

function createCalendarPromise(calendarId) {
   var dfd = $.Deferred();
   gapi.client.calendar.events.list({
        'calendarId': calendarId,
         ... Your code...
   }).then(function(response) {
      ... Your Code, push events to a global list...
      dfd.resolve();
  }).fail(function() { dfd.resolve();});

  return dfd.promise
}
函数callAllCalendars(){
var承诺=[]
var calendarIdList=[“calendar1”、“calendar2”、“calendar3”];
对于(var i=0;i
function callAllCalendars() {
   var promises = []
   var calendarIdList = ["calendar1", "calendar2", "calendar3"];
   for(var i = 0; i < calendarIdList .length, i++){
      promises.push(createCalendarPromise(calendarIdList [i]));
   }
   $.when.apply($, promises).done(function () {
      ... Your code to do the sorting ...
   });

}

function createCalendarPromise(calendarId) {
   var dfd = $.Deferred();
   gapi.client.calendar.events.list({
        'calendarId': calendarId,
         ... Your code...
   }).then(function(response) {
      ... Your Code, push events to a global list...
      dfd.resolve();
  }).fail(function() { dfd.resolve();});

  return dfd.promise
}