0

我有一个脚本,它创建一个名为eventData1. 在事件部分,events创建。我想动态地完成这项工作,而不是静态地放在 html 文件中。

    var eventData1 = {
          options: {
            timeslotsPerHour: 4,
            timeslotHeight: 20,
            defaultFreeBusy: {free: true}
          },
          events : 
            {'id':1, 'start': new Date(year, month, day, 12), 'end': new Date(year, month, day, 13, 30), 'title': 'Lunch with Mike', userId: 0},
            {'id':2, 'start': new Date(year, month, day, 14), 'end': new Date(year, month, day, 14, 45), 'title': 'Dev Meeting', userId: 1},
            {'id':3, 'start': new Date(year, month, day+1, 18), 'end': new Date(year, month, day+1, 18, 45), 'title': 'Hair cut', userId: 1},
            {'id':4, 'start': new Date(year, month, day+2, 8), 'end': new Date(year, month, day+2, 9, 30), 'title': 'Team breakfast', userId: 0},
            {'id':5, 'start': new Date(year, month, day+1, 14), 'end': new Date(year, month, day+1, 15), 'title': 'Product showcase', userId: 1}
          ]
    };

因此,我的文件创建了另一个名为 的变量appointments,其设置与您在上面看到的完全一样:

[{'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}]

如何用我的appointments变量替换静态创建的事件?

我试过了,但没有奏效:

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

编辑:马特是对的,约会变量在范围内,但尚未设置:

  var appointments = "[";
  var counter = 0;
   $.getJSON('link', 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}";

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

        appointments += "]";

    console.log(appointments);
    });

知道在其他代码使用它之前我可以做些什么来设置它吗?

4

1 回答 1

0

我对文件中代码的哪一部分有点困惑,但我猜你向服务器发出请求,然后设置eventData1并处理服务器响应。

Javascript 字符串是不可变的,因此当您修改时appointments eventData1.events不会更新。看这个例子:

var str = "hi";
var obj = {myString: str}; // obj.myString = "hi"
str = "hello"; // obj.myString is still "hi"

“hi”字符串不更新,因为它是不可变的,它被一个新字符串覆盖。

因此,您可以做的一件事是eventData.events显式更新并为其分配新值appointments

但是既然appointments是 Javascript 数组的字符串表示,为什么不使用数组来代替呢?所以而不是

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

利用

appointments = []; // Do this outside of the $.each
... 
appointments.push(appointments += "{'id': " + id + ", 'start': new Date( '" + start+ "'), 'end': new Date('" + end + "'), 'title': 'test appointment from javascript', userId: 0}");
于 2013-01-07T19:01:56.853 回答