我有一个脚本,它创建一个名为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);
});
知道在其他代码使用它之前我可以做些什么来设置它吗?