FullCalendar:最初不是从函数调用(AJAX)渲染的事件

时间:2014-02-11 16:20:56

标签: javascript jquery ajax node.js fullcalendar

我已将FullCalendar配置为从AJAX请求中提取其事件,但在首次加载页面时,它们不会在日历上呈现。

$(document).ready(function() {

    sh1client = new Array();
    sh2client = new Array();

    $('#sh1_cal').fullCalendar({

         height: 1000,
         minTime:'9:00am',
         maxTime:'5:00pm',
         editable: false,

         events: function(start, end, callback) {

            $.ajax({
                type: 'GET',
                url: 'http://localhost:8080/getEvents',
                dataType: 'json',
                success: function(reply) {

                    console.log(reply.first);
                    callback(reply.first);

                }
            });
        }
    });


    $("#sh1_cal").fullCalendar('addEventSource', sh1client);   // these are the clientside arrays

});

在服务器上,

app.get('/getEvents', function(req, res){

    console.log('Server: passing events...');

    var arrays = {first: sh1, second: sh2}
    var pack = JSON.stringify(arrays)

    res.writeHead(200, {'Access-Control-Allow-Origin' : '*', 'Content-Type': 'application/json'});
    res.end(pack);

});

这些事件最初无法加载是否有任何原因?似乎所有东西都没有通过,但这就像回调无法正常工作一样。

编辑:这是我尝试的另一种方法

events: { 

            url: 'http://localhost:8080/getEvents',
            type: 'GET',

            error: function() {
                alert('there was an error while fetching events!');
            },

            success: function(reply) {
                console.log(reply);
                //callback(reply.first);
            },

            color: 'yellow',   // a non-ajax option
            textColor: 'black' // a non-ajax option

         }

编辑:JavaScript控制台会在加载后将其显示为已发布到页面(这是数组中的第一个对象:

[Object]
allDay: "false"
end: "1392129000"
id: "phil@google.com"
room: "Sh1"
start: "1392127200"
title: "Phil - Google"
__proto__: Object
length: 1
__proto__: Array[0]

1 个答案:

答案 0 :(得分:4)

您是否尝试过使用fullcalendars而不是使用自己的ajax调用?

http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

Fullcalendar将dataType默认为JSON并将其缓存为false。

将部分代码与doc:

中的代码结合使用
$('#calendar').fullCalendar({

    events: {
        url: 'http://localhost:8080/getEvents',
        type: 'GET',
        error: function() {
            alert('there was an error while fetching events!');
        },
        success: function(reply) {
            console.log(reply.first);
            callback(reply.first);
        },
        color: 'yellow',   // a non-ajax option
        textColor: 'black' // a non-ajax option
    }

});

你可以试着让你的JSON字符串剪切并粘贴,看看你是否可以在没有ajax调用的情况下进行渲染

     events: [
     {
            end: 1392129000,
            id: "phil@google.com",
            room: "Sh1",
            start: 1392127200,
            title: "Phil - Google"
      }]

您还可以处理回复:

$('#myCalendar').fullCalendar({
...
   eventSources : [{
      url: 'myUrl',
      type: 'GET',
   },
   success: function(replyObject) {
      var results = [];
      var reply= replyObject.Results[0];
      for(var index in reply) {
         results.push(reply[index]);
      }
      return results;
    }
    }]
...