Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way in which I would do this is 3 fold.</p> <ol> <li>Encapsulate javascript in small well-defined classes within namespaces</li> <li>Javascript classes should have HTML they require "injected" into them as dependency allowing <a href="https://stackoverflow.com/questions/300855/looking-for-a-better-javascript-unit-test-tool">out-of-browser unit testing</a></li> <li>Move as much client-side functionality to server as possible and use a concept known as AHAH</li> </ol> <p><strong>Javascript name-spacing</strong></p> <p>This can be achieved easily and has been covered in other posts such as this <a href="https://stackoverflow.com/questions/13021/is-there-a-concise-way-to-do-namespacing-in-javascript">Is there a &quot;concise&quot; way to do namespacing in JavaScript?</a></p> <p><strong>Small encapsulated classes</strong></p> <p>Javascript code, just like server-side code should be well encapsulated with small cohesive classes and methods. Each class lives in a separate file, named along with the namespace it is in, eg: MyCompany.SomePackage.MyClass.js. Excessive HTTP requests to each file can be saved via <a href="https://stackoverflow.com/questions/841253/javascript-good-tool-to-minify-jquery-based-js-files">combining and minifying</a> these class files at build time.</p> <p><strong>Dependency Inversion in Javascript</strong></p> <p>So effectively instead of selecting the elements you require to work with inside your class, like this:</p> <pre><code>var MyNamespace.MyClass = function() { var elementINeed = $('#IdOfElementINeed'); } </code></pre> <p>You would inject it as such:</p> <pre><code>var foo = new MyNamspace.MyClass($('#IdOfElementINeed')); var MyNamespace.MyClass = function(incomingDependency) { var elementINeed = incomingDependency; } </code></pre> <p>This technique lends itself well to testable javscript and seperation of concerns through <a href="https://stackoverflow.com/questions/386885/is-there-a-javascript-mvc-micro-framework">MVC style</a> layering of your code.</p> <p><strong>AHAH and Client-side simplification</strong></p> <p><a href="http://www.xfront.com/microformats/AHAH.html" rel="nofollow noreferrer">AHAH</a> is quite an old technique that has been around for quite some time in web-development, although is making a resurgence amongst web aficionados for its pure simplicity. However, the philosophy must be bought into at more than the architectural technique level and it must be used as a replacement for all your client side javascript eg: validation, showing/hiding dynamic content, calculations etc</p> <p>Where you may used to have attached an onClick event with client-side complexity:</p> <pre><code>$('#someElement').click(function(){ // insert complex client-side functionality here, such as validating input // eg var isValid = $(this).val() &lt; minimumCommentLength; // update page based on result of javascript code // eg $('#commentTooLong').show(); }) </code></pre> <p>Now you would simply trigger an ajax request back to the server to get the new HTML and simply replace all or some of the elements you are interested in as such:</p> <pre><code>$('#addCommentButton').click(function(){ $.ajax({ url: "/comment/add", context: document.body, success: function(responseHTML){ $('body').html(reponseHTML); }}); }) </code></pre> <p>Obviously this is a trivial example, but when used effectively, ANY javascript event on the page, simply fires off the identical ajax request and HTML replacement, greatly reducing the amount of client-side code required. Moving it to the server where it can be effectively tested.</p> <p>AHAH nay-sayers, will argue that this is not a performant way to run a web-site, however I have used and seen this technique on sites with 56k modem access and also massively scaled public web-sites. The result is of course slower, but you can still produce sub 100 millisecond round trips, which is practically instant to humans.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload