02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

280<br />

CHAPTER 8 ■ EDITING THE CALENDAR WITH AJAX AND JQUERY<br />

■ Note Using the “month-” prefix for the ID means that you stay compliant with W3 st<strong>and</strong>ards, which state that<br />

element IDs must begin with a letter.<br />

Building Date Objects in JavaScript<br />

To ensure that the new event falls within the current month, create two empty Date objects: one for the<br />

current month <strong>and</strong> one for the new event.<br />

To set the value of the current month’s Date object, retrieve the ID attribute from the H2 element<br />

using the .attr() method, split it at the hyphens, <strong>and</strong> store it in the cdata variable.<br />

For the new event, split the value of entry.event_start at the spaces <strong>and</strong> take the first array element<br />

(which is the date in the format of YYYY-MM-DD) <strong>and</strong> store it in a variable called date. Next, split the<br />

information at the hyphens <strong>and</strong> store the array in a variable called edata.<br />

To set the Date objects, use the data from cdata <strong>and</strong> edata to set the date in cal <strong>and</strong> event,<br />

respectively.<br />

Finally, modify fx.addevent with the following bold code:<br />

fx = {<br />

"initModal" : function() {...},<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...},<br />

// Adds a new event to the markup after saving<br />

"addevent" : function(data, formData){<br />

// Converts the query string to an object<br />

var entry = fx.deserialize(formData),<br />

},<br />

// Makes a date object for current month<br />

cal = new Date(NaN),<br />

// Makes a date object for the new event<br />

event = new Date(NaN),<br />

// Extracts the calendar month from the H2 ID<br />

cdata = $("h2").attr("id").split('-'),<br />

// Extracts the event day, month, <strong>and</strong> year<br />

date = entry.event_start.split(' ')[0],<br />

// Splits the event data into pieces<br />

edata = date.split('-');<br />

// Sets the date for the calendar date object<br />

cal.setFullYear(cdata[1], cdata[2], 1);<br />

// Sets the date for the event date object<br />

event.setFullYear(edata[0], edata[1], edata[2]);

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!