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.

$.validDate = function(date, options)<br />

{<br />

// code here<br />

};<br />

})(<strong>jQuery</strong>);<br />

CHAPTER 10■ EXTENDING JQUERY<br />

The options object will only have one property: the pattern to be used for validation. Because you<br />

want the options object to be optional, you define a default value for the pattern in the function <strong>by</strong><br />

inserting the following bold code:<br />

(function($){<br />

// Extends the <strong>jQuery</strong> object to validate date strings<br />

$.validDate = function(date, options)<br />

{<br />

// Sets up default values for the method<br />

var defaults = {<br />

"pattern" : /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/<br />

};<br />

};<br />

})(<strong>jQuery</strong>);<br />

Extending Default Options with User-Supplied Options<br />

You can extend the default object using the $.extend() function, which will create a new object <strong>by</strong><br />

combining the default options with the user-supplied options. If there are three options available <strong>and</strong><br />

the user passes an object with only two of them defined, using $.extend() will only replace the two<br />

properties redefined <strong>by</strong> the user.<br />

Insert the code shown in bold to extend the default object:<br />

(function($){<br />

// Extends the <strong>jQuery</strong> object to validate date strings<br />

$.validDate = function(date, options)<br />

{<br />

// Sets up default values for the method<br />

var defaults = {<br />

"pattern" : /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/<br />

},<br />

};<br />

})(<strong>jQuery</strong>);<br />

// Extends the defaults with user-supplied options<br />

opts = $.extend(defaults, options);<br />

347

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

Saved successfully!

Ooh no, something went wrong!