You need to sign in to do that
Don't have an account?
Richard Houston GMU
Start a timer when child component loads
Hi,
I'm trying to add a timer to individual cases when they're added to the queue and displayed in a lightning component. I thought I could simply fire an "init" on the child component to execute a JS timer. However, it's not loading the function.
Thanks for any help!
Business need, display a list of cases in the queue and how long they have been in that queue.
Child component name: Case Tile
Parent component name: Case List
Child component:
Parent component:
Parent helper:
I'm trying to add a timer to individual cases when they're added to the queue and displayed in a lightning component. I thought I could simply fire an "init" on the child component to execute a JS timer. However, it's not loading the function.
Thanks for any help!
Business need, display a list of cases in the queue and how long they have been in that queue.
Child component name: Case Tile
Parent component name: Case List
Child component:
<aura:component > <aura:attribute name="case" type="case"/> <aura:registerEvent name="navigateToSObject" type="force:navigateToSObject"/> <aura:registerEvent name="selectSObject" type="ltng:selectSObject"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <lightning:card > <aura:set attribute="title" > <a onclick="{!c.caseSelected}" style="color: black; "> <div class="slds-align_absolute-center"> Name: <aura:if isTrue="{#v.case.Contact.Name!=undefined}"> {#v.case.Contact.Name} <aura:set attribute="else"> {#v.case.SuppliedName} </aura:set> </aura:if> </div> </a> </aura:set> <a onclick="{!c.caseSelected}" style="color: black; "> <p class="slds-p-horizontal--small"> Requested Advisor: {#v.case.Requested_Advisor_Counselor__c} </p> <p class="slds-p-horizontal--small"> Type: {#v.case.Type} </p> <p class="slds-p-horizontal--small"> Time in queue: <ui:outputNumber value="{#v.case.Total_minutes_in_the_queue__c}" /> </p> </a> </lightning:card> </aura:component>Child controller:
({ doInit : function(component){ console.log('Do an action but nothing happens'); } })
Parent component:
<aura:component controller="CaseTileController" implements="flexipage:availableForAllPageTypes" access="global" > <aura:attribute name="cases" type="Case[]" /> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <aura:handler event="c:QueueChange" action="{!c.onQueueChange}" /> <aura:attribute name="selectedQueue" type="string" /> <c:streaming channel="/event/CaseChange__e" onMessage="{!c.eventFired}" /> <lightning:layout horizontalAlign="left" class="slds-theme_shade"> <lightning:layoutItem padding="around-small" size="12" smallDeviceSize="6" mediumDeviceSize="4" largeDeviceSize="3" > <lightning:icon iconName="standard:case" size="medium" alternativeText="Case Queue" class="slds-p_small" /> <span class="slds-text-heading_medium"> Case Queue</span> </lightning:layoutItem> <lightning:layoutItem padding="around-small" size="12" smallDeviceSize="6" mediumDeviceSize="4" largeDeviceSize="3" > <c:QueueSelector /> </lightning:layoutItem> </lightning:layout> <lightning:layout horizontalAlign="left" multipleRows="true" title="Case Queue" class="slds-theme_shade"> <aura:if isTrue="{!(v.cases != null)}" > <aura:iteration items="{!v.cases}" var="c" > <lightning:layoutItem padding="around-small" size="12" smallDeviceSize="6" mediumDeviceSize="4" largeDeviceSize="3" > <c:caseTile case="{#c}" /> </lightning:layoutItem> </aura:iteration> <aura:set attribute="else"> <div class="slds-align_absolute-center" > <div class="empty-state-container slds-text-align_center" > <img src="/projRes/ui-home-private/emptyStates/noTasks.svg" /> <div class="empty-state-message slds-m-top_medium" style="margin-bottom: 1rem;" > Check back later, there are no current cases in the queue. </div> </div> </div> </aura:set> </aura:if> </lightning:layout> </aura:component>Parent controller:
({ doInit : function(component, event, helper) { helper.getCases(component); }, eventFired : function(component, event, helper){ helper.getCases(component); }, onQueueChange: function(component, event, helper){ component.set("v.selectedQueue", event.getParam("selectedQueue")); helper.getCases(component); } })
Parent helper:
({ checkForChanges : function(component, event, helper) { var action = component.get("c.getAvailableCases"); action.setCallback(this,function(data){ component.set("v.cases",data.getReturnValue()); }); $A.enqueueAction(action); }, getCases : function(component, page){ var action = component.get("c.getAvailableCases"); action.setParams({ "queueSelection" : component.get("v.selectedQueue") }); action.setCallback(this,function(response){ var state = response.getState(); if(state === "SUCCESS"){ var results = response.getReturnValue(); component.set("v.cases",results); } else if (state === "INCOMPLETE") { // do something } else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(action); } })
Could you please replace <c:caseTile case="{#c}" /> with <c:caseTile case="{!c}" /> and let me know if it solves the issue ?
Thanks for the reply. I made the modificatoin, however the caseTileController is still not firing the init.
Could you please make below changes :-
1- Add an boolean Attribute - <aura:attribute name="isDataReturned" type="boolean" default="false"/>
2-Change getCases function in your helper :-
component.set("v.cases",results);
component.set("v.isDataReturned", true);
3- Change your aura if on line no 17 to - <aura:if isTrue="{!v.isDataReturned}">
Let me know if these resolves it or not.
That still didn't get the init function to fire. But it did improve the page speed! Which is a good thing.
Are you getting the message "Do an action but nothing happens" in your console log?
Current markup of caseTileController:
Current markup of CaseListHelper:
CaseListController:
Sure thing here's the apex controller:
What is the purpose of streaming component ?
The streaming component is to handle new/updated cases and to refresh the component. It is from this project:
https://github.com/afawcett/streamingcomponent/tree/master
caseTile Component markup:
caseTileController:
caseTileHelper:
caseList component:
caseListController:
caseListHelper:
caseTile
CaseList
Just for the Info :- I commented out the <c:streaming> and <c:QueueSelector> as i did not have the same in my org. This caused the init function to fire(Probably). Request you to try once commenting them in your code and check.
https://salesforce.stackexchange.com/questions/156214/call-function-in-child-component-from-the-parent-component/156243
- Added an aura:method to the child component
- Updated the parent markup to include an aura:id on the iterated component.
- Added a for loop in the parent helper to fire the method for each item in the iteration.
Here's the updated code:caseTile:
caseList component:
caseListHelper:
That's great to hear. The only think i am concerned about is that whenever a new component is loaded doInit is fired automatically. Even though with this approch desired results are obtained but this was a very strange behaviour.
But glad it worked out for you. Cheers !
just one thing change <aura:handler name="init" value="{!v.case}" action="{!c.doInit}" /> to <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> in caseTile component.