8

I have a class hierarchy starting with abstract class and going multiple levels deep. Every class in the hierarchy has a number of @AuraEnabled attributes.

Top level class

public abstract class MyAbstractClass
{
    @AuraEnabled
    public String attFromAbstractClass { get; set; }
    public MyAbstractClass()
    {
        attFromAbstractClass = 'hello';
    }
}

Class that extends it

public class MyClass extends MyAbstractClass
{
    @AuraEnabled
    public String myAtt { get; set; }

    public MyClass()
    {
         super();
         myAtt = 'world';
    }
}

Controller class

public class CmpCtrl
{
    @AuraEnabled
    static public MyAbstractClass getResult()
    {
         return new MyClass();
    }
}

Aura Application

<aura:application extends="force:slds" controller="CmpCtrl">
    <aura:handleEvent name="init" value="{!this}" action="{!c.init}" />
</aura:application>

Aura Application Controller

({
    init: function(cmp,evt,h) {
       var action = cmp.get('c.getResult');
       action.setCallback(this, function(servResp){
           var state = servResp.getState();
           if(state === 'SUCCESS') {
               console.log(servResp.getReturnValue());
           }
       }, 'ALL');
       $A.enqueueAction(action);
    }
})

The output of the server response is an object with only myAtt visible. How do I access attFromAbstractClass from the response?

1 Answer 1

6

I have had the same issue, I was using inheritance on one of my return types to Lightning.

The only option I saw was to serialize the response in the APEX controller and deserialize it back in the JS Controller.

So in your Apex Controller:

public class CmpCtrl
{
    @AuraEnabled
    static public String getResult()
    {
         return JSON.serialize(new MyClass());
    }
}

In your Javascript controller:

({
    init: function(cmp,evt,h) {
       var action = cmp.get('c.getResult');
       action.setCallback(this, function(servResp){
           var state = servResp.getState();
           if(state === 'SUCCESS') {
               var rValue = JSON.parse(servResp.getReturnValue())
               console.log(rValue);
           }
       }, 'ALL');
       $A.enqueueAction(action);
    }
})

Hope this helps you. It's not ideal but it works when using inheritance.

2
  • Thank you. was hoping the issue would be highlighted to the devs as it adds an unnecessary performance overhead with serializing and parsing. I will mark your answer as accepted a bit later in hope that someone from Salesforce looks at this issue, too.
    – Artur
    Nov 30, 2016 at 15:10
  • 1
    That means that server side inheritance is not supported on Lightning. This is a real pain Dec 1, 2016 at 9:37

You must log in to answer this question.

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