<p>We have an application with a good amount of jQuery JSON calls to server side code. Because of this, we have a large amount of binding code to parse responses and bind the appropriate values to the form. This is a two part question.</p>

<ol>
<li><p>What is the reccomended approach for dealing with a large number of forms that all have different data. Right now were are trying to take a structured approach in setting up a js "class" for each page, with an init, wireClickEvents etc.. to try to have everything conformed.</p></li>
<li><p>Is there any "best practices" with creating repetitive jQuery code or any type of reccomended structure other than just throwing a bunch of functions in a js file?</p></li>
</ol>

[{'answer_id': 34753, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 3, 'selected': True, 'text': '<p>Not 100% sure example what you are asking, but personally, and I use MochiKit, I create JavaScript "classes" (or widgets, if you prefer) for every significant client-side UI structure. These know, of course, how to populate themselves with data.</p>\n\n<p>I don\'t know what more there is to say - writing UI code for the browser in JavaScript is no different than writing UI code for other types of apps, as far as I am concerned. Build classes and instantiate them as needed, populate them with data, have them throw events, etc. etc.</p>\n\n<p>Am I up in the night on this? :)</p>\n\n<hr>\n\n<p>EDIT: In other words, yes - do what you are doing, for the most part. I see too many novice JavaScript hackers write a bunch of poorly-cohesive functions that don\'t appear to be a part of anything specific other than they are all in a single file. Hope that makes sense. </p>\n'}, {'answer_id': 4629484, 'author': 'ordnungswidrig', 'author_id': 9069, 'author_profile': 'https://Stackoverflow.com/users/9069', 'pm_score': 0, 'selected': False, 'text': '<p>I think there are multiple challanges for you. The first question is how to structure javascript code, i.e. how to build namespaces so that you don\'t fight name clashes or have to name your functions like </p>\n\n<pre><code>form1validate\nform1aftersubmit\nform2validate\nform2aftersubmit\n</code></pre>\n\n<p>One of the proven patterns for modules in javascript is to use an anonymous function to build a new naming scope. The basic idea is shon in the following code</p>\n\n<pre><code>(function() {\n  var foo = 1;\n})();\n\n(function() {\n  if(foo == 1) alert("namespace separation failed!")\n})();\n</code></pre>\n\n<p>I think this <a href="http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth" rel="nofollow">blog entry</a> is a good introduction.</p>\n\n<p>The second question you face is how to avoid all the repetition in javascript code. \nYou have a couple of weapons against this.</p>\n\n<ol>\n<li>functions - this seams obvious but it\'s often forgotten to refactor common code into functions where it can be done. In you case this will be functions to copy values from the json response into the forms and like that</li>\n<li>higher order function - or functions as data - or callback, as they are often called in javascript. These are the mightiest weapon in javascript. In case for form and ajax handling you can use callback to avoid repetition in the control flow of your forms. </li>\n</ol>\n\n<p>Let me construct an example out of my head (using jquery for convinence)</p>\n\n<pre><code>// this is a validator for one form\n   var form1validator = function() {\n     if($("input[name=name]",this).attr("value").length &lt; 1 &amp;&amp;\n        $("input[name=organisation]",this).attr("value").length &lt; 1)\n       return "Either name or organisation required" \n   }\n\n   // and this for a second form\n   var form2validator = function() {\n     if($("input[name=age]",this).attr("value").length &lt; 21\n       return "Age of 21 required"\n   }\n\n   // and a function to display a validation result\n   var displayResult = function(r) {\n     $(this).prepend("&lt;span&gt;&lt;/span&gt;").text(r);\n   }\n\n   // we use them as higher order functions like that\n\n   $("#form1").onSubmit(validator(form1validator, displayResult, function() {\n     //on submit\n     ...send some xhr request or like that\n   });\n\n   $("#form2").onSubmit(validator(form2validator, displayResult, function() {\n     this.submit() // simply submit form\n   });\n\n   $("#form1b").onSubmit(validator(form1validator, function(r) {\n     alert("There was an validation error " + r);\n   }, function() {\n     //on submit\n     ...send some xhr request or like that\n   });\n\n\n   // the validator function itself would be defined as\n\n   function validator(formValidator, displayResult, onSubmit) {\n     var r = formValidator.apply(this)\n     if(typeof(r) === \'undefined\')\n       onSubmit(this)\n     else\n       displayResult(r)\n   }\n</code></pre>\n'}, {'answer_id': 4629701, 'author': 'Chris Westbrook', 'author_id': 16891, 'author_profile': 'https://Stackoverflow.com/users/16891', 'pm_score': 3, 'selected': False, 'text': '<p>You should probably look into a framework like <a href="http://www.knockoutjs.com" rel="noreferrer">knockout.js</a>  This way you can just update your models and the forms will update automatically.</p>\n'}]
['https://Stackoverflow.com/questions/34705', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2993/']