2

I am implementing a Custom Community using Napili template. I am displaying custom object's record list on one of the Standard community pages.

Is there a built-in functionality or setting that enables to refresh the page every five minutes? That way, community users can get the latest records in the list every five minutes?

EDIT: I have implemented this Lightning component with doInit function:

Lightning Component:

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global">
       <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
 </aura:component>

JS Code:

({
doInit: function(cmp) {
    window.setTimeout(
        $A.getCallback(function() {
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
                "url": '/first'
            });
            urlEvent.fire();
        }), 5000
    );
  }
})

This code refreshes only once after the page initial load, how can I achieve refreshing the page every time-interval?

P.S.: How can I exclude refreshing whenever someone is creating a Related Record for the records that are present on this community page? Example: This so-called: 'Refresh Page' will open up with bunch of records. So once I click on any of the records to add/create a related record, the page should NOT get refreshed while I am in the half-way of record creation.

  • Would there really be that much change to the data in a five minute period? Also won't users see it if they navigate back to the list view anyway as would they really just sit with the object list view open? – Dave Humm Aug 21 '17 at 14:40
  • Yes, data changes(or new records will be added) anytime. Its kind of a notification record that will have very sensitive information and Customers need to act on them immediately. – Arnold Jr. Aug 21 '17 at 14:44
4

Create a custom lightning component for communities to do this .You will need to ensure this component is part of every Napili lightning page that you want to refresh

Here is a version of untested code but hoping it gets you going

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global">
   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
   <aura:attribute name="win" type="Integer" />
</aura:component>

Controller:

({
  doInit: function(cmp) {
    var win = window.setInterval(
       $A.getCallback(function() {
         window.location.reload();// Try reloading here
      }), 300000
     );

     cmp.set("v.win",win);
   }
})

Renderer:

({
    unrender: function (cmp, helper) {
        this.superUnrender();
        window.clearInterval(cmp.get("v.win"));
    }
})

Here's the extract from the answer which explains the need of unrender:

setInterval loop will continue to run because it is attached to the window, but the lifecycle of your component is not the life cycle of the window. E.g. if your component is unrendered, say because a user closes the tab in which your component was running, the window object will still be there running that interval long after your component is gone.

That's why you should clean up by calling clearInterval when your component is unrendered, and that's something you can do in a custom unrender() handler

  • Mohith, this is working just fine. I see the complete browser's tab getting reloaded(thanks to window.locaton.reload( )). But, is it possible to load only the content of the page(not the complete browser reload) using: urlEvent.fire(); – Arnold Jr. Aug 21 '17 at 18:26
  • I have implemented this, which refreshes only one time: ({ doInit: function(cmp) { window.setTimeout( $A.getCallback(function() { var urlEvent = $A.get("e.force:navigateToURL"); urlEvent.setParams({ "url": '/first' }); urlEvent.fire(); }), 5000 ); } }). Please let me know what I am missing here. – Arnold Jr. Aug 21 '17 at 19:00
  • 2
    Lightning front end is Javascript .So look for Javascript documentation .Look in MDN site developer.mozilla.org/en-US/docs/Web/API/… .Please accept the answer if it helped and help the community – Mohith Shrivastava Aug 21 '17 at 19:50
  • 1
    @MohithShrivastava You need to clear the setInterval after the component is destroyed. Please use the unrender of the renderer so that it doesn't live outside of the scope of the component's life cycle. – Praveen Aug 22 '17 at 3:55
  • 1
    @ArnoldJr. Please check the updated the answer. – Praveen Aug 28 '17 at 5:36
5

Alright, with the guidance from @Mohith, I was able to find a work around for my issue. Here is what I made changes to his code which reloads the complete page, rather refresh the part of the page(reload itself, seems dynamic refresh) based on time interval. Thanks to Lightning Component as well.

Lightning Component:

<aura:component implements="forceCommunity:availableForAllPageTypes" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

JS Code:

({
doInit: function(cmp) {
    window.setInterval(
        $A.getCallback(function() {
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
                "url": '/'
            });
            urlEvent.fire();
        }), 600000
    );
  }
})

Hope this helps someone who visits this post.

0

The only built in functionality is the refresh button that a user can click to refresh the list view. This is only visible when using the Full Layout.

Icons

  • Can I implement any custom component that sits on every page(hidden) and makes the page to get refreshed every 5 minutes? – Arnold Jr. Aug 21 '17 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.