我有两种类型的事件,即"holidays"one_answers"events",它们应该从两个变量中接收。变量包含ajax响应。现在,我想将节日和活动作为"活动"传递到日历中。我不知道如何传递变量。下面是我的ajax代码,它被声明为变量。我的代码正确吗?
var return_holidays = function() {
var holdays = [];
$.ajax({
url: "/calendar/show_holidays",
type: 'POST', // Send post data
data: 'type=fetch',
async: true,
success: function(s)
{
//alert(s);
holdays = s;
}
});
return holdays;
}();
var return_events = function() {
var dynamic_events = [];
$.ajax({
url: "calendar/show_events",
type: 'POST', // Send post data
data: 'type=fetch_events',
async: true,
success: function(s)
{//alert(s);
dynamic_events = s;
}
});
return dynamic_events;
}();
$('#calendar').fullCalendar({
utc: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
editable: true,
droppable: true,
eventSources: [return_holidays, return_events],//am calling the variables
eventAfterRender: function(event, element, view) {
element.append(event.title);
}
});
我能以这种方式实现吗?这是正确的吗?
return_holidays,return_events的返回类型应该是JSON,所以您可以尝试返回JSON.stringify(yourArray);
,而不是纯数组。您也可以尝试让日历处理提取业务:
$('#calendar').fullCalendar({
eventSources: [
{
url: '/calendar/show_holidays',
type: 'POST',
data: {
'type' : 'fetch'
},
error: function() {
alert('there was an error while fetching holidays!');
},
},
{
url: '/calendar/show_events',
type: 'POST',
data: {
'type' : 'fetch_events'
},
error: function() {
alert('there was an error while fetching events!');
},
}
]
});
http://fullcalendar.io/docs/event_data/events_json_feed/
或者,您可以将URL直接传递到日历:
$('#calendar').fullCalendar({
eventSources: [
'/calendar/show_holidays',
'calendar/show_events'
]
});
http://fullcalendar.io/docs/event_data/eventSources/
始终确保从后端以JSON格式重新调整对象。