我有两种类型的活动,即假期'和'事件',应该从两个变量接收。变量持有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);
}
});
我能以这种方式实现吗?这是对的吗?
答案 0 :(得分:3)
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/
或者,您可以将网址直接传递给日历:
$('#calendar').fullCalendar({
eventSources: [
'/calendar/show_holidays',
'calendar/show_events'
]
});
http://fullcalendar.io/docs/event_data/eventSources/
始终确保从后端重新调整JSON格式的对象。