0

Can we check whether a lightning event of type COMPONENT was fired or not.

I have an event which is handled in parent component with 3 attributes.

Say Ocr,CI,CD..

Now what is happening is these events are fired one at a time and they are getting true or false values based on some condition.

Here when Ocr = true CD,CI are getting undefined and when CD is true Ocr,CI are undefined and CI is true Ocr,CD are undefined. So i need to check if events which are firing 3 times got fired and set some values.

FROM CONSOLE LOG (Ocr,CD,CI are for example purpose)

checkOcr undefined
checkCd true
checkCi undefined

checkOcr undefined
checkCd undefined
checkCi true

checkOcr true
checkCd undefined
checkCi undefined

So when all 3 variables become true then i need to something..I even tried using counter variable but at 2nd time it is initializing the count value again to 0. Is it possible to check ?

  • Little confused here, Are you taking about three independent events here? Or same event setting 3 different attributes! – codeyinthecloud Aug 21 '18 at 15:37
1

As I understand that you have write aura:handler in parent component and also motioned the action attribute and pass a function of component controller (suppose fireEventHandlerMethod). When ever event will fire controller method will execute.

So, you have create a attribute in parent component like

<aura:attribute name="messageFromEvent" type="Object" default = "{Ocr : false, CI : false, CD : false}"/>

and in the controller method (fireEventHandlerMethod) set the value of messageFromEvent attribute like:

you got Ocr = true from event than you set true to Ocr of messageFromEvent attribute and for other also

messageFromEvent attribute will persist all the values and than you can do some operation on this.

  • Yes chandra i am doing same thing here..But as events is getting called 3 times from 3 different components each time ocr,CI,CD are getting undefined and i cannot check for 'success for all three at a time. – Deepak Agarwal Aug 23 '18 at 8:27
  • Like i mentioned in my Question section the values i am getting in console log – Deepak Agarwal Aug 23 '18 at 8:28
0

You need to "remember" if the event was fired.

<aura:attribute name="ocr" type="Boolean" default="false" />
<aura:attribute name="ci" type="Boolean" default="false" />
<aura:attribute name="cd" type="Boolean" default="false" />

componentEventHandler: function(component, event, helper) {
  var params = event.getParams();
  if(params.ocr !== undefined) {
    component.set("v.ocr", true);
  }
  if(params.ci !== undefined) {
    component.set("v.ci", true);
  }
  if(params.cd !== undefined) {
    component.set("v.cd", true);
  }
  if(['ocr','ci','cd'].reduce((a,v) => a && component.get("v."+v), true)) {
    // all 3 are true, do something now
  }
}

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.