Usage of Liferay JS Object

Liferay comes with very usefull js utilities that ease life of Developer. smiley

Liferay provides  js object "Liferay" which has modularized structure. We are going to discuss on this more here.

Let's see what all submodules it has, by image shown below.


 

Let's try to understand these modules one by one.

Liferay.AUI:
It has functions to get aui/js paths.

 

Liferay.Address:

It has functions to get countries and regions(based on country code).

Liferay.Address.getCountries() returns all active countries in form of JSON object.
Liferay.Address.getRegions(1,COUNTRYCODE) returns all regions for COUNTRYCODE.

Liferay.Address is available only for logged-in context, not for Guest user.
Also as these function uses services to fetch countries, regions, you will require html\js\liferay\service.js available to context where you are using these functions.

Liferay.Browser
Following image shows functions provided by Liferay.Browser.


Most of them are self-explanatory.
It gives you information about client like browser name, its version, OS, Gzip compression acceptance, device info.

Liferay.Data:
I didnt get much on this part, but it seems it is used to store data.
For example:
Lifeary.Data.userId="10183"; and afterwards using Lifeary.Data.userId anywhere.

Liferay.Language:
Using Liferay.Language.get(key,extraParam), you can get language property based on key.
It will return language property based on current locale.

e.g. Liferay.Language.get('read-more') will return you Read More string.

Liferay.Portlet:
Liferay.Portlet.refresh can be used to refresh portlet.
For Example:
Liferay.Portlet.refresh('#p_p_id_58_') will refresh login portlet.

You can also bind portlet ready and portlet onload events.
Liferay.Portlet.ready(function(portlet){});
Liferay.Portlet.onload(function(portlet){})

You can remove portlet from layout using below code.
Liferay.Portlet.close('#p_p_id_58_') will remove portlet(here sign in portlet) from page.

You can open Configuration of any portlet using below method:
Liferay.Portlet.openConfiguration('#p_p_id_58_','',url)

Following image shows functions related to Liferay.Portlet.


 

 

Liferay.ThemeDisplay:
Liferay.ThemeDisplay provides you themeDisplay object having much userful methods to get information about current context.
For Example: companyId, current layout information, user information, current group information etc.
Most of functions shown in below image are self-explanatory.


Liferay.Util:
Liferay.Util is having many utility methods commonly used. Follwing are some of them.

e.g

      Liferay.Util.check, Liferay.Util.checkAll will used to check/uncheck checkboxes attached to form.

      Liferay.Util.endsWith,Liferay.Util.startsWith to perform string operation.

      Liferay.Util.escapeHTML, Liferay.Util.unescapeHTML  to escape/unescape html from string.

     Liferay.Util.isArray to check if js object is of type array.

     Liferay.Util.randomInt, Liferay.Util.randomMinMax to generate random number.

     Liferay.Util.toNumber for casting string to number.

 

 

You can get information about methods by running Liferay.Util in browser's javascript console.

 

NOTE: All of the screenshots above are taken using Chrome's developer console.

Hope this blog would be helpful to someone.

 

Blogs
Thanks Pankaj, I think Liferay Javascript Utilities should be documented in the "developer documentation". There are a lot of usefull functions, that could make life easier and I don't know any place where to find documentation about it...
Dear Pankaj,

Honestly this is one area which is not yet documented well. Your blog is the first step towards giving a shape for the subject and making the developers aware of the incredible power of Liferay Javascript Library. Due to lack of documentation, developers seldom use these functions. Thanks for sharing and appreciate if you can add more contents to this topic. Even in my Cookbook, i tried my level best to capture as much information as possible around this topic. But i feel i have not done full justice given the full depth of the subject.

Ahamed Hasan
Author, Liferay Cookbook
http://mpowerglobal.com/download-cookbook
Hi Pankaj,

I have a query. I have used the Liferay.Portlet.refresh() to refresh the portlet. But after it gets refreshed some operations that used to happen before dont seem to be happening.

I am new to Liferay, so could you help me out by telling what all events occurs after the refresh takes place?

Or better yet, could you point me to the right resources.?