繁体   English   中英

控制器未从闪电组件初始化处理程序接收对象

[英]Controller not receiving Object from Lightning Component Init Handler

我有一个包含数据表的闪电手风琴,该表未接收要填充该表的对象数据。 该组件获取Case对象,将其存储为recordData并将其发送到我的控制器类。 它应显示父帐户的所有子域的数据表,如下面的示例所示。 在此处输入图片说明

CONTROLLER CLASS:
public class CollectionCaseDomainsController {

    @AuraEnabled
    public static List<Domain__c> queryDomains(Case CurrentCase) {        
        // if (CurrentCase.AccountId == null) { return;}

        //query all the children accounts (if any)
        Set<Id> allAccountIds = new Set<Id>{CurrentCase.AccountId};
        Boolean done = false;
        Set<Id> currentLevel = new Set<Id>{CurrentCase.AccountId};
        Integer count = 0;
        while(!done) {
            List<Account> children = [ SELECT Id FROM Account WHERE Parent.Id IN :currentLevel ];
            count++;
            currentLevel = new Set<Id>();
            for (Account child : children) {
                currentLevel.add(child.Id);
                allAccountIds.add(child.Id);
            }

            //added in a count, to prevent this getting stuck in an infinate loop
            if (currentLevel.size() == 0 || count > 9) {
                done = true;
            }
        }

        //query the assets
        List<Asset> assets = [ SELECT Domain__c FROM Asset WHERE AccountId IN :allAccountIds ];

        Set<Id> domainIds = new Set<Id>();
        for (Asset a : assets) {
            domainIds.add(a.Domain__c);
        }

        return [ SELECT Name FROM Domain__c WHERE Id IN :domainIds ];
    }
}
LIGHTNING COMPONENT:
<aura:component controller="CollectionCaseDomainsController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="CurrentCase" type="Case" />
    <aura:attribute name="Domains" type="List" />
    <aura:attribute name="Columns" type="List" />

    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <force:recordData aura:id="caseRecord"
                      recordId="{!v.recordId}"
                      targetFields="{!v.CurrentCase}"
                      layoutType="FULL"
                      />

    <lightning:accordion activeSectionName="Domains">

        <lightning:accordionSection name="Domains" label="Domains">

            <lightning:datatable data="{ !v.Domains }" columns="{ !v.Columns }" keyField="Id" hideCheckboxColumn="true"/>

        </lightning:accordionSection>

    </lightning:accordion>
</aura:component>
COMPONENT CONTROLLER:
({
    doInit : function(component, event, helper) {
        component.set("v.Columns", [
            {label:"Domain Name", fieldName:"Name", type:"text"}
        ]);

        var action = component.get("c.queryDomains");

        action.setParams({
            CurrentCase: component.get("v.CurrentCase")
        });

        action.setCallback(this, function(data) {
            var state = data.getState();
            if (state === "SUCCESS") {
                component.set("v.Domains", data.getReturnValue());
            } else {
                console.log("Failed with state: " + state);
            }
        });

        // Send action off to be executed
        $A.enqueueAction(action);
    }
})

您的问题对数据出错的地方并没有太多的具体说明,但是我对这个问题有一个有根据的猜测:您在init处理程序和force:recordData自己的加载过程之间存在竞争条件。

doInit() ,您执行

    action.setParams({
        CurrentCase: component.get("v.CurrentCase")
    });

但是v.CurrentCase由您的<force:recordData>填充:

<force:recordData aura:id="caseRecord"
                  recordId="{!v.recordId}"
                  targetFields="{!v.CurrentCase}"
                  layoutType="FULL"
                  />

异步加载。 您无法保证<force:recordData>在调用init处理程序之前将(一次或每次)完成其加载。

幸运的是,您不需要这样。 只需将v.recordId属性传递到服务器端控制器,然后在此执行您需要的所有SOQL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM