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?

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.

| improve this answer | |
  • 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 '16 at 15:10
  • 1
    That means that server side inheritance is not supported on Lightning. This is a real pain – Itai Shmida Dec 1 '16 at 9:37

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.