Liferay PortletURL in JavaScript

by Eduardo Lundgren on March 27, 2009, views: 1457

Each more we feel the need of becoming more and more web 2.0, it sounds like a physics law to the "guys" that are trying to survivor arround the amazing applications that surround us across the urls.

Since few months ago I want to blog post about the new Liferay functionality that let you build PortletURLs using JavaScript only.

There are many places you can apply this new way of generating PortletURLs. Imagine a big list of links on your portlet, instead of download repeated hundreds urls from the server site you can simply create a javascript function that retuns a PortletURL instance and set on the fly the parameters you need. And now, you can simply integrate your pure javascript files (.js) with PortletURLs, without over passing it as parameter for your javascript constructor or another work-arround came from our misterious mind.

The usage of this functionality using JavaScript is very simple:

var portletURL = new Liferay.PortletURL();
portletURL.setParameter("key1", "value");
portletURL.setParameter("key2", "value");
portletURL.setPortletId(86);

alert( "that is the url: " + portletURL.toString() );

 

If you want to know what methods you can play with the JavaScript Liferay.PortletURL you can take a quick look on the methods below:

 

setCopyCurrentRenderParameters: function(copyCurrentRenderParameters);
setDoAsUserId: function(doAsUserId);
setEncrypt: function(encrypt);
setEscapeXML: function(escapeXML);
setLifecycle: function(lifecycle);
setName: function(name);
setParameter: function(key, value);
setPlid: function(plid);
setPortletConfiguration: function(portletConfiguration);
setPortletId: function(portletId);
setPortletMode: function(portletMode);
setResourceId: function(resourceId);
setSecure: function(secure);
setWindowState: function(windowState);
toString: function();

 

I've also created some shortcuts for diferent kind of urls on the portal:

 

var actionURL = Liferay.PortletURL.createActionURL(); // = new Liferay.PortletURL('ACTION_PHASE');

var renderURL = Liferay.PortletURL.createRenderURL(); // = new Liferay.PortletURL('RENDER_PHASE');

var resourceURL = Liferay.PortletURL.createResourceURL(); // = new Liferay.PortletURL('RESOURCE_PHASE');

var permissionURL = Liferay.PortletURL.createPermissionURL(portletResource, modelResource, modelResourceDescription, resourcePrimKey);

 

 I hope it helps.


Post a Comment » | Social Bookmarks » | Posted in Liferay liferay, javascript

6 Comments

by sdfs on 28/09/2009 23:27
sdf
by Jeroen Dierckx on 29/04/2009 01:30
Hey Eduardo,
Thanks very much for verifying. Do you have any idea when the fix will be released?
by Eduardo Lundgren on 17/04/2009 13:44
Hi Jeroen, that's a bug, I will fix this and let you know, Thank you a lot.
by Jeroen Dierckx on 16/04/2009 04:55
Hi Eduardo,
The problem i'm having is that when i construct a url with javascript, the url does not comply with one generated with the actionURL tag of the portlet tag lib.

I found these differences in the parameters (TL is tag generated, JL is javascript generated link):

1) a set of underscores too many surrounding the portlet id:
TL: ?p_p_id=Foo_WAR_Bar_INSTANCE_s38I
JL: ?p_p_id=_Foo_WAR_Bar_INSTANCE_s38I_

TL: &_Foo_WAR_Bar_INSTANCE_s38I_javax.portlet.action=something
JL: &__Foo_WAR_Bar_INSTANCE_s38I__javax.portlet.action=something

2) url type parameter is different:
TL:&p_p_url_type=1
JL: &p_p_url_type=0
Is there a method to change this url type maybe?

These difference result in the action not getting called...
by Eduardo Lundgren on 11/04/2009 12:40
Hi Jeroen Dierckx,

It's added by the URL Factory automatically and there is no way to disable this, at least until now. Is there some problem in having this _ on the keys? when you get this parameter on the backend you can retrieve this using the real name, without the _.

Let me know if it helps.

-- Eduardo
by Jeroen Dierckx on 10/04/2009 02:41
Hi Eduardo,
Thanks a lot for the convenience of using pure javascript to construct a liferay URL.
I'm running into a problem with it though; is it expected that the keys get prependend with "_[portlet id]_"?
Is there a way to disable this?

Thanks in advance for your time.