Google Calendar javascript api - 添加多个事件 [英] Google Calendar javascript api - Add multiple events

查看:124
本文介绍了Google Calendar javascript api - 添加多个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过javascript v3 api将多个事件添加到Google Calendar时遇到了问题。



我有一个数组,其中有条目事件如下:


  newEvent = {
summary:response [i] .name +BDay !!,
start:{
dateTime:date
},
end:{
dateTime:date
}
};

events [i] = newEvent;


之后,我打电话给Google Calendar api,以便添加事件:

  var request; 
for(var j = 0; j< events.length; j ++){

console.log(events [j]);

request = gapi.client.calendar.events.insert({$ b $'calendarId':calendarId,
'resource':events [j]
});
request.execute(function(resp){
console.log(resp);
});
}

然而,事实证明,所有事件都放置在同一日期日历(实际上是数组事件[]中的最后一个日期)。我相信这可能是因为请求是回调函数,但我不确定。



感谢您的帮助!

解决方案

事件[j] 正在for循环的每次迭代中被反弹。尝试使用匿名函数绑定到正确的事件:

  var request; 
for(var j = 0; j< events.length; j ++){

console.log(events [j]);

request = function(resource){//返回请求的函数。
return gapi.client.calendar.events.insert({$ b $'calendarId':calendarId,
'resource':resource
});
}(events [j]); //绑定到当前事件。
request.execute(function(resp){
console.log(resp);
});



$ b $ p
$ b

有关JavaScript数组和闭包的更多详细信息,请参阅以下问题:循环中的JavaScript闭包 - 简单实用的例子



以下是一个易于阅读的上述代码版本,它将所有处理过程转换为函数:

  var makeRequest = function(resource){
console.log(resource);
var request = gapi.client.calendar.events.insert({$ b $'calendarId':calendarId,
'resource':resource
});
request.execute(function(resp){
console.log(resp);
});
}; (var j = 0; j< events.length; j ++){
makeRequest(events [j]);


}


I've been facing a problem when I'm trying to add multiple events to a Google Calendar via javascript v3 api.

I have an array which entries are events like these:

newEvent = {
    "summary": response[i].name+" BDay!!",
    "start": {
      "dateTime": date
    },
    "end": {
      "dateTime": date
    }
  };

  events[i]=newEvent;

After, I make a call to Google Calendar api in order to add the events:

var request;
for(var j = 0; j<events.length; j++) {

  console.log(events[j]);

  request = gapi.client.calendar.events.insert({
    'calendarId': calendarId,
    'resource': events[j]
  });
  request.execute(function(resp) {
   console.log(resp);
 });
}

However, it turns out that all the events are placed in the same date into the calendar(which actually is the last date in array events[]). I believe that it could be because the requests are callback functions, but I'm not sure.

Would appreciate the help!

解决方案

events[j] is being rebound on each iteration of the for loop. Try using an anonymous function to bind to the correct event:

var request;
for(var j = 0; j<events.length; j++) {

  console.log(events[j]);

  request = function(resource) {  // Function that returns a request.
    return gapi.client.calendar.events.insert({
      'calendarId': calendarId,
      'resource': resource
    });
  }(events[j]);  // Bind to the current event.
  request.execute(function(resp) {
    console.log(resp);
  });
}

See the following question for more details on JavaScript arrays and closures: JavaScript closure inside loops – simple practical example

Here is an easier-to-read version of the code above that moves all the processing into a function:

var makeRequest = function(resource) {
  console.log(resource);
  var request = gapi.client.calendar.events.insert({
    'calendarId': calendarId,
    'resource': resource
  });
  request.execute(function(resp) {
    console.log(resp);
  });
};

for(var j = 0; j<events.length; j++) {
  makeRequest(events[j]);
}

这篇关于Google Calendar javascript api - 添加多个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆