Search code examples
ajaxliferayliferay-7

Liferay 7.2 - Resource URL is calling same instance


I have two or more equal portlets on the same page. And the portlet itself has a Resource URL request.

So let's suppose that I have on the same page my portlet_pages_INSTANCE_1 and portlet_pages_INSTANCE_2, and the relevant code for the portlet is:

JSP:

<liferay-portlet:resourceURL var="loadDocuments" />

<i class="fa fa-angle-double-left" onclick="paginate()"></i>

<script>
    function paginate() {
        $.ajax({
            type: 'POST',
            url: '${ loadDocuments }',
            data: {
                '<portlet:namespace />someData': someData
            },
            success: function(data) {
                // handle the success
            }
        });
    }
</script>

This works wonders if only one instance of the same portlet is in the page. But it has a strange behaviour if more than one of the same portlet is added to the page.

After some debugging I tried to:

@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException, PortletException {
    System.out.println(themeDisplay.getPortletDisplay().getInstanceId());
}

And noticed that dispiting clicking on the paginate button on different portlet instances, the serveResource prints only the latest portlet that I added instance.

Is there any way to make the ajax call to the a specific portlet instance?


Solution

  • You have multiple definitions of your paginate method per resulting HTML page. You'll need to namespace it, or parameterize a common implementation.

    For javascript, one of those method overloads the previous