4

Trying to read currently clicked source's title using event.getSource().get("{!v.title}") and set it to the Event attribute. But it is showing me below error.

Action failed: c$AssignedAssessmentListItemToPatient$controller$fireatq [SyntaxError: Invalid key {!v.title}] Failing descriptor: {c$AssignedAssessmentListItemToPatient$controller$fireatq}. Please try again.

It was working fine before Summer 16 release. Please find below code:

fireatq : function(component, event, helper) {
   var action = $A.get("e.c:ATQEvent");
   action.setParams({"AssId" : event.getSource().get("{!v.title}")}
   action.fire();
}
  • try get("v.title") instead of get("{!v.title}") – Praveen Jun 13 '16 at 9:51
1

Since Lightning Components Bundle version 39 or above, this seems to have been stable now.

If you have an event like onclick on an HTML tag like <div>, you can use event.currentTarget.

But if the event is for a Lightning Component tag like <lightning:button>, you need to use event.getSource().

This has been well documented in the Event Handling section of Lightning Components Developer guide.

0

I found that you can assign entire objects and then get them via event.currentTarget. Not sure how long is it going to work for, but works with LS in v40:

<lightning:button
    label="View"
    name="view"
    onclick="{!c.toggleView}"
    variant="{! if(v.state.screen == 'view', 'brand', 'neutral') }"/>


toggleView: function(cmp, event, helper) {
  const view = event.currentTarget.name
  cmp.set('v.state.screen', view)
}

OR

<lightning:button
    label="Foo"
    name="{!v.someObject}"
    onclick="{!c.baz}"/>

baz: function(cmp, event, helper) {
  console.log(event.currentTarget.name)
}

Unsure if this answers your question, but it's a nice and a useful pattern to use.

  • 1
    I think something has quietly changed and... it doesn't seem to work anymore. – dzh Jul 4 '17 at 23:07

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.