"The difference between a good developer and a great developer can be summed up in a single word: elegance," according to Kevin St. Clair.
JavaScript is a staple in any modern portlet development project. But the reluctance to properly reference and define JavaScript objects within the program has the potential to create a number of problems when code that might work locally moves into production. This tutorial will focus on how to define, implement and namespace JavaScript and JavaScript objects properly, a practice that will help to avoid misuse, common pitfalls and problems that often occur when portal applications go live.
Here are a couple of best practices when working with portlets:
Remember that administrators can add multiple portlets, or multiple instances of the same portlet, to the same page. Here are solutions to help you onyour WebSphere Portal development journey.
This approach makes my portlets infinitely cleaner and eliminates unreadable JSP files full of poorly misused or unreadable JavaScript code.
In this article, I plan to answer the following questions:
Before we proceed, please referencehow to define a portlet namespace, where I introduce a more readable solution when working with namespaces and context paths in JSPs.
<c:set var="ns" scope="page" value="${renderResponse.getNamespace()}" /> <c:set var="cp" scope="page" value="${renderRequest.getContextPath()}" />
|
view.jsp
Always limit the use of JavaScript in a JSP. The only JavaScript in the JSP should be written to configure and instantiate JavaScript objects that will do the actual work. The bulk of the JS should live in a separate JavaScript file and be included in the JSP as demonstrated below:
<script src="${cp}/js/leone.js"></script>
|
view.jsp
(Note: I've placed my JS files under the/WebContent/js/ directory in the portlet web application project.)
Next, we include a<div> element identified by the portlet namespace. This is the placeholder where all the portlet HTML will be placed.
<div id=”${ns}”> <!—Portlet HTML structure to be included here. --> </div> |
view.jsp
Below is a breakdown of the JavaScript module pattern I use to define my custom JavaScript objects. It offers the following advantages:
Below is a stripped down template of the Leone object following the module pattern. All private methods are defined within the_privatevariable, and all public methods are defined within the highlightedreturn block.
Pay attention to thenamespace parameter in the object definition as it is available to all public and private methods within theLeone object.
A more complete example is provided below:
In the above example, I declare thens helper variable as part of my Leone object. This is particularly helpful when making JQuery selector calls.
Here's an example of how to create a unique portlet instance of the Leone JavaScript object for use throughout the portlet.
(Note: I could technically pass in the namespace parameter as one of the configuration parameters, but I prefer to keep it explicitly defined so I don't forget about it.)
I hoped to share this pattern for defining and namespacing JavaScript objects, as I personally have found itparticularly useful for portlets and JavaScript. This approach makes my portlets infinitely cleaner and eliminates unreadable JSP files full of poorly misused or unreadable JavaScript code.
What tips do you have for integrating JavaScript into your enterprise applications?Let us know.
Next Steps:
Five portlet development tips
Avoid PortletPreferences for performance
Respecting the servlet API
An ADR is only as good as the record quality. Follow these best practices to establish a dependable ADR creation and maintenance ...
At some point, all developers must decide whether to refactor code or rewrite it. Base this choice on factors such as ...
API proxies and gateways help APIs talk to applications, but it can be tricky to understand vendor language around different ...
A principal engineer says Postman's Spec Hub will help the company shift to a spec-first API development process for its ...
AWS Kiro, a project developed by a 'small, opinionated team within AWS,' prioritizes spec-driven development in the workflows of ...
Docker is expanding the Docker Compose spec to accommodate AI agents in an effort to bring AI development closer to existing ...
AI is transforming PaaS with automation and cost-efficient features, but will it eventually replace cloud platforms? Industry ...
Even though Q-Day might be several years away, enterprises should develop a strategic plan to prepare for the future. Experts ...
Businesses can find security vulnerabilities when they push their workloads to the edge. Discover the pitfalls of cloud edge ...
Runtime security and tokenization stand to play a bigger role in attack surface management, a development that could influence ...
How CISOs design and build their security teams is as important as the technology they select to safeguard their organizations' ...
Calculating and communicating cybersecurity ROI can help persuade top management to invest. Here's how to use meaningful, ...
Compare Datadog vs. New Relic capabilities including alerts, log management, incident management and more. Learn which tool is ...
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...