+

Search Tips   |   Advanced Search

JavaScript namespacing - observing good practice


The client side context of Web programming requires good namespacing.

When using the client side programming model for the portlets you need to apply good namespacing practices in JavaScript. This helps to avoid collisions in the page which can be timely and expensive to track down. The namespace need must be balanced with the performance advantage of moving all JavaScript into external JavaScript file that can be cached. An additional benefit is that this makes portlet JSPs much smaller. Refer to the following code sample of writing JavaScript for a portlet by the client side programming model:

htmlEditor.js:

if ( typeof( HTMLEditorController ) == "undefined" ) {
     var HTMLEditorController = function( namespace ) {
          this._namespace = namespace;
          this.getSaveForm = function () {
               return document.forms[ namespace + "saveForm" ]
          }
     }
}

htmlEditor.jsp:

var <%=namespace%>htmlController = new HTMLEditorController( "<%=namespace%>" );


Parent: The client side portlet programming model
Related:
Get started with the client side programming model for portlets
Handling portlet preferences on the client
Change portlet mode and window state on the client side