Design Patterns for adding jQuery features to a form control in PHP/jQuery (or other web languages) [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this questionI am using PHP and jQuery for a project.
A开发者_JAVA百科s some forms are reused across different functionality, I've coded a strategy class to render the forms. An example of one such class looks something like this:
class strategy_render_member_form()
{
public function execute() {
return '<form>
<input type="text" name="user_name" id="user_name"></input>
//...snipped
<input type="text" name="dob" id="dob"></input>
</form>';
}
}
A controller feeds this output to a view
class controller_register()
{
public function show_form() {
// ..snipped
$view->form = $this->strategy_render_register_form->execute();
}
}
I wish to format the 'dob' textbox as a calendar, using jQuery's DatePicker. Now the question is - where should I add the JavaScript function to do so?
<script type='text/javascript'>
$(function() {
$( "input#dob" ).datepicker();
</script>
Should the controller add it to the view? Should the strategy define it? Should I use a callback or hook to 'decorate' the form control?
IMO, this is best kept isolated from PHP as a Progressive Enhancement. It belongs to the UI behavior layer, e.g. into your JavaScript files. I'd register the clickhandler unobtrusively on page load in an external JS file (think bootstrap for JS), along with any other JavaScript that enhances the UI.
精彩评论