jQuery weekcalendar:无法调用未定义的方法'getTime'

时间:2013-01-07 20:18:11

标签: javascript jquery json

我正在使用jQuery Weekcalendar而不是静态数据,我想使用数据库中的数据。下面的脚本创建了eventData1变量,其中包含日历的所有约会。当我用手粘贴下面的输出时,一切正常。当我通过变量添加它时,它会崩溃并显示以下错误消息:

Uncaught TypeError: Cannot call method 'getTime' of undefined 

所以我认为它无法将日期转换为新的Date对象。这就是为什么我尝试使用一个单独的函数来将字符串转换为日期,但没有成功。

events : [{'id': 25, 'start': new Date( '2013-01-07 14:45:00'), 'end': new Date('2013-01-07 15:45:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 26, 'start': new Date( '2013-01-10 11:15:00'), 'end': new Date('2013-01-10 12:15:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 22, 'start': new Date( '2013-01-09 12:45:00'), 'end': new Date('2013-01-09 14:45:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 21, 'start': new Date( '2013-01-09 15:00:00'), 'end': new Date('2013-01-09 17:00:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 20, 'start': new Date( '2013-01-08 16:00:00'), 'end': new Date('2013-01-08 17:00:00'), 'title': 'test appointment from javascript', userId: 0},{'id': 27, 'start': new Date( '2013-01-10 15:45:00'), 'end': new Date('2013-01-10 16:45:00'), 'title': 'test appointment from javascript', userId: 0}]

这是我的JavaScript:

  <script type="text/javascript">

      function dateReviver(value) {
        if (typeof value === 'string') {
          var re = /^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/
          var result = re.exec(value);
          if (result) {
              return new Date(Date.UTC(+result[1], +result[2] - 1, +result[3], +result[4],+result[5], +result[6]));
          }
        }
        return value;
      }

      var appointments = "[";
      var eventData1 = new Array();
      var counter = 0;

      $.ajax({
        url: 'http://www.slinder.ch/admin/php/termin_getappointments.php',
        dataType: 'json',
        async: false,
        success: function(data) {

            console.log('entered getJSON()');
            console.log(data);
            $.each(data, function(i, appointment) {
                var id = appointment.appointmentId;
                var start = appointment.start;
                var end = appointment.end;
                var title = appointment.prename;
                var userid = appointment.workerid;

                appointments += "{'id': " + id + ", 'start': new Date( '" + start + "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}";

                console.log(start);
                console.log(end);

                if (i === (data.length - 1)) {
                    // this is the last                    
                } else {
                    appointments += ",";
                }             
                counter++;
            });

            appointments += "]";
            console.log('first: ' + appointments);

            eventData1 = {
                options: {
                  timeslotsPerHour: 4,
                  timeslotHeight: 20,
                  defaultFreeBusy: {free: true}
                },
                events : appointments,

                freebusys: []
            };
        }
  });

  </script>

任何人都可以告诉我为了使用这样的约会我必须改变什么:

events : appointments,

在SO上有类似的question,但也没有经过批准的答案。

1 个答案:

答案 0 :(得分:2)

您正在appointments变量中构建一个字符串。因此,它不能用作数组。相反,使用对象构建一个普通的JavaScript数组。

var appointments = [];
....
appointments.push({
    id: id,
    start: new Date(start),
    end: new Date(end), 
    title: 'test appointment from javascript', 
    userId: 0
});
...

编辑:

或者您可以简单地从服务器推送您已获得的数据,因为它看起来并不像您实际上正在修改它。如果您只是简单地重命名键,那可能不值得付出额外的努力。