JavaScript namespacing - observing good practice

 

+

Search Tips   |   Advanced Search

 

When using the client side programming model for your portlets 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 files that can be cached. An additional benefit is that this makes portlet JSPs much smaller.

Here is a JavaScript sample 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 topic

The client side portlet programming model

 

Related tasks


Getting 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