WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Thursday, June 14, 2012

Javascript - My programming guidelines

JavaScript in general becomes hard to maintain for large projects when the code gets fragmented. So as a general rule of thumb following guidelines are what I follow:

Javsacript files
  • All javascript files must be placed in /content/JS folder
  • All plugins must be placed in /content/JS/plugins folder
    • Plugins are files that are extensions to default UI or binding behavior.
    • Example: Autocomplete, Watermarking, Lazyloading, livequery etc.
  • When building a custom UI behavior that potentially could be used in other pages should be placed in /contents/JS/Shared folder.


Shared UI functionality
In situations where functionality has subtle differences, option based extension model is preferred to avoid duplication of code. For example:



Javascript Classes
  • This would help in avoiding function name collisions.
  • Each view probably should have its own javascript class using {view name} as the naming convention. This class must hold all local variables and functions associated to that view.

Two ways of defining classes in javascript:


Inline Scripting / UI Binding
  • No inline scripting must be used
  • No html based inline property scripting must be used either. For example: "input type=”test” value=”hello world” class=”cs1” id=”test1” onClick=”callSomeFunction();” "
  • Instead all binding must be declared in the class’s init() function. The init() function could/should be called in document.ready() method.

Labels: ,