Set timer on AUI and send request on ActionURL over AJAX

Vikash Kumar
Vikash Kumar
Less Than a Minute Read

setInterval(function() {
                    var url = '<portlet:actionURL><portlet:param name="javax.portlet.action" value="getXXXMethod" /><portlet:param name="userid" value="<%=String.valueOf(userid)%>" /><portlet:param name="isGuest" value="<%=String.valueOf(isGuest)%>" /></portlet:actionURL>';  
                    AUI().use('aui-base','aui-io-request', function(A){
                    A.io.request(url,{dataType: 'json',method: 'GET',
                        data: {param:"timezone"},                     
                        on: {  
                          failure: function() { },  
                          success: function(data) {
                              var data = this.get('responseData');                        
                              A.Array.each(data, function(obj, idx){
                                  var location = obj.area;
                                  var timeValue =obj.timeZone;
                              });
                          }
                        }  
                    });
                });
            },60*1000);

Page Comments
For ajax request, ServeResource is good option. Also userId and isGuest can be fetch from ThemeDisplay in server side so you do not need to pass it in request. Here is updated code :

<portlet:resourceURL var="getXXXURL" id="getXXX">
</portlet:resourceURL>
<script>
setInterval(function() {
var url = '<%=getXXXURL.toString()%>';
AUI().use('aui-base','aui-io-request', function(A){
A.io.request(url,{dataType: 'json',method: 'GET',
data: {param:"timezone"},
on: {
failure: function() { },
success: function(data) {
var data = this.get('responseData');
A.Array.each(data, function(obj, idx){
var location = obj.area;
var timeValue =obj.timeZone;
});
}
}
});
});
},60*1000);
</script>

and in Java class:

@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException,
PortletException {
ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
String resourceID = resourceRequest.getResourceID();
long userId = themeDisplay.getUserId();
long defaultUserId = themeDisplay.getDefaultUserId();
//do what you want to do
}

Related Assets...

No Results Found

More Blog Entries...