Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Ajax calls liferay portlets

I am having liferay portlet and I need to heavily depend upon the AJAX calls. So I need to make multiple calls to serveResource method. One way to do the same is that I can pass a parameter with the URL and then differentiate the request according to that parameter.

But in my case I have to call serveResource so many times due to which the method will be difficult to maintain. Is there any framework to do so? Using which the code becomes maintainable.

like image 949
Danish Avatar asked Oct 03 '22 18:10

Danish


2 Answers

Use Spring MVC framework and call different method based on your business logic/user action in controller,

Try below code in jsp

<portlet:resourceURL var="loadContents" id="loadContents"></portlet:resourceURL>
<portlet:resourceURL var="loadCategories" id="loadCategories"></portlet:resourceURL>

ajax call in jsp

AUI().ready(
        function(A) {            
            A.use('aui-io-request', 
                    function(aui) {
                    A.io.request("<%=loadContents%>", {
                        autoLoad : false,
                        cache : false,
                        dataType : 'json',
                        data:{},
                        method:'POST',
                        on : {
                            success : function(event, id, xhr) {
                                var response = this.get('responseData');
                                 // add logic here after response
                            }
                        }
                    }).start();
                });
        });

in controller/ java class

    @ResourceMapping("loadCategories")
    public void loadCategories(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
    {
         // your business logic goes here
    }

    @ResourceMapping("loadContents")
    public void loadContents(final ResourceRequest resourceRequest, final ResourceResponse resourceResponse)
    {
         // your business logic goes here
    }

hope above code snippets will help you and you get what you were looking for!!!

like image 151
Pritesh Shah Avatar answered Oct 12 '22 12:10

Pritesh Shah


Adding more in this.We can not use the serveResource method like processAction.There can be multiple processAction in single liferay portlet(Which is not spring mvc portlet) ,while in case of serveReource it will be single.

serveResource is mainly used for ajax call.We can handle multiple ajax request in serveResource method by differentiating the call using resource Id.

resourceRequest.getResourceID() will return the Id, which we have defined in jsp using below code.

<portlet:resourceURL var="demoUrl" id="demoUrl"></portlet:resourceURL>
like image 35
Krutik Jayswal Avatar answered Oct 12 '22 12:10

Krutik Jayswal