3

I am trying the code below(most of it from the Dev Docs here)

Component

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="stages" type="String[]" default="Any,Open,Closed,Closed Won"/>
<aura:iteration items="{!v.stages}" var="stage">
 <ui:inputRadio label="{!stage}" change="{!c.onRadio}" />
</aura:iteration>

<b>Selected Item:</b>
<p><ui:outputText class="result" aura:id="radioResult" value="" /></p>

<b>Radio Buttons - Group</b>
<ui:inputRadio aura:id="r0" name="others" label="Prospecting" change="{!c.onGroup}"/>
<ui:inputRadio aura:id="r1" name="others" label="Qualification" change="{!c.onGroup}" value="true"/>
<ui:inputRadio aura:id="r2" name="others" label="Needs Analysis" change="{!c.onGroup}"/>
<ui:inputRadio aura:id="r3" name="others" label="Closed Lost" change="{!c.onGroup}"/>

<b>Selected Items:</b>
<p><ui:outputText class="result" aura:id="radioGroupResult" value="" /></p>
</aura:component>

Controller

({
onRadio: function(cmp, evt) {
     var selected = evt.source.get("v.label");
     var resultCmp = cmp.find("radioResult");
     resultCmp.set("v.value", selected);
 },

 onGroup: function(cmp, evt) {
     var selected = evt.source.get("v.label");
     var resultCmp = cmp.find("radioGroupResult");
     resultCmp.set("v.value", selected);
 }
})

But, when I choose different radio buttons, I get the error:

Cannot read property 'get' of undefined

which basically means evt.source is undefined.This works without locker service and hence it has something to do with locker

Any ideas?

5
  • Whats the API version of your lightning component? What IDE you are using to create this ? Works fine in my spring 17 org Feb 25, 2017 at 16:49
  • @MohithShrivastava Ohh, thats strange! My component is on 39.0 and I am using the dev console and I am doing this is a Spring'17 Org too. Feb 25, 2017 at 16:55
  • Locker activated ? Hope it's not locker issue.In my org locker is activated Feb 25, 2017 at 17:00
  • Narrowed it .Looks like a locker regression .Works without locker on Feb 25, 2017 at 17:02
  • Cool! Yup, Locker Service is activated. And Yes, I switched off the locker service and it is working now. What could be the solution? Feb 25, 2017 at 17:06

1 Answer 1

5

Yes so the issue is Locker expects that we write our Javascript in Strict mode and also as per new release the right method to use is using getSource on an event object

enter image description here

Here is the working code

({
 onRadio: function(cmp, evt) {
   var selected = evt.getSource().get("v.label");
   var resultCmp = cmp.find("radioResult");
   resultCmp.set("v.value", selected);
 },

 onGroup: function(cmp, evt) {
    var selected = evt.getSource().get("v.label");
    var resultCmp = cmp.find("radioGroupResult");
    resultCmp.set("v.value", selected);
   }
})

Just raised an issue with Doc team to update the docs to use getSource() instead of source in the docs.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .