jaredatch/Custom-Metaboxes-and-Fields-for-WordPress — Issue #78: Extracting objects from scripts
Author: GaryJones
Created: 2011-12-31 18:09:21+00:00
State: open

Consider the following current code:

```
$('.cmb_timepicker').each(function (){
$('#' + jQuery(this).attr('id')).timePicker({
startTime: "07:00",
endTime: "22:00",
show24Hours: false,
separator: ':',
step: 30
});
});
```
The arguments on how the timepicker should run are embeded within the timepicker call, which is inside an anonymous function that run on each item in a jQuery collection.

Better would be to pull this object out and identify it:

```
var timepicker_args = {
startTime: "07:00",
endTime: "22:00",
show24Hours: false,
separator: ':',
step: 30
};

$('.cmb_timepicker').each(function (){
$('#' + jQuery(this).attr('id')).timePicker(timepicker_args);
});
```
The timepicker args could potentially be re-used, conditionally amended somewhere between their definition and their usage (e.g. `timepicker_args.step = 20;`), and you might see how some values might be pulled in to populate it from the PHP via `wp_localize_script()`.

The datepicker args might also be done in the same way - it might even be passed in completely from the PHP side, allowing the date format, say, to be easily filterable there before it arriving at the script.