views:

29

answers:

3

I am trying to use jMonthCalendar to add some events from an XML feed into a calendar. In the original jMonthCalendar, the events are in an array that looks like this:

var events = [
{ "EventID": 1, "StartDateTime": new Date(2009, 5, 12), "Title": "10:00 pm - EventTitle1", "URL": "#", "Description": "This is a sample event description", "CssClass": "Birthday" },
{ "EventID": 2, "StartDateTime": "2009-05-28T00:00:00.0000000", "Title": "9:30 pm - this is a much longer title", "URL": "#", "Description": "This is a sample event description", "CssClass": "Meeting" }];

I am using a loop to create a bunch of events like this:

eventsArray += '{"EventID":'+eventID+', "StartDateTime": '+new Date(formattedDate)+', "EndDateTime":  '+new Date(formattedDate)+', "Title": "'+eventTitle+'", "URL": "'+detailURL+'","Description": "'+description+'"},'

I am then trying to get these back into an array by performing

eventsArray = eventsArray.slice(0, -1); var events = [eventsArray];

The problem is, the stuff in "eventsArray" does not get converted back into array objects, like it does in the example source.

I know that this is a noob question, but any help would be appreciated.

+1  A: 

Instead of using += and the string version of the object, try appending the actual object.

For example, instead of:

eventsArray += '{"EventID":'+eventID+', "StartDateTime": '+new Date(formattedDate)+', "EndDateTime":  '+new Date(formattedDate)+', "Title": "'+eventTitle+'", "URL": "'+detailURL+'","Description": "'+description+'"},'

Do:

events.push({"EventID":eventID, "StartDateTime": new Date(formattedDate), "EndDateTime": new Date(formattedDate), "Title": eventTitle, "URL": detailURL,"Description": description});
Jon Weers
Wow, thanks! I ended up using push instead of append, but awesome answer. Thanks!
mrr0ng
A: 

Change your creation loop:

eventsArray.push({
  EventID: eventID, 
  StartDateTime: new Date(formattedDate), 
  EndDateTime:  new Date(formattedDate),
  Title: eventTitle, 
  URL: detailURL,
  Description: description
});
gnarf
A: 

I believe you will get what you want by switching from string concatenation to manipulating the objects directly:

var newEvent = {"EventID": eventID, "StartDateTime": new Date(formattedDate), "EndDateTime": new Date(formattedDate), "Title": eventTitle, "URL": detailURL, "Description": description};
events.push(newEvent);
sunetos