3

I want to create generic lightning component. I created table to simply show data by passing in to it object name and field set. And created server-side controller to get this data by object name and field set. But in some cases I need to add buttons to this table like 'Edit', 'Delete' or something else. I know that I can extend lightning component to do so, but can I connect another server-side controller to handle this buttons? Or am I going to need to copy existed base component and server-side controller to do so?

0

As far as I can tell, the way to do this would be to extend the components themselves, giving each component it's own server-side controller. I don't have a lot of expertise in the Object-Oriented side of Lightning Components, but I was able to throw together a rough example that I hope will provide some insight at least:

Aura Application

<aura:application >
    <c:ReadAndWrite />
</aura:application>

ReadAndWrite.cmp

<aura:component controller="Write" extends="analysis:Read">
    <ui:button label="Button" press="c.updateFirstAccount"/>
</aura:component>

ReadAndWriteController.js

({
    updateFirstAccount : function(component, event, helper) {
        var action = component.get("c.updateAccounts");
        var Accounts = component.get("v.Accounts");
        Accounts[0].Name = new Date().toString();
        action.setParams({alist:Accounts});
        action.setCallback(this,function(response){
            if (response.getState() === 'SUCCESS'){
                console.log(response.getReturnValue());
                component.set("{!v.Accounts}",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

Write.apxc

public class Write {
    @AuraEnabled
    public static list<Account> updateAccounts(list<Account> alist){
        update alist;
        return alist;
    }
}

Read.cmp

<aura:component controller="Read" extensible="true">
    <aura:attribute name="Accounts" type="Account[]" />
    <aura:handler name="init" value="{!this}" action="c.retrieveAccounts" />
    <aura:iteration items="{!v.Accounts}" var="i">
        {!i.Name} <br/>
    </aura:iteration>
    {!v.body}
</aura:component>

ReadController.js

({
    retrieveAccounts : function(component, event, helper) {
        var action = component.get("c.retrieveAccountsFromServer");
        action.setCallback(this,function(response){
            if (response.getState() === 'SUCCESS'){
                console.log(response.getReturnValue());
                component.set("{!v.Accounts}",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    },
})

Read.apxc

public class Read {
    @AuraEnabled
    public static list<Account> retrieveAccountsFromServer(){
        return [
            SELECT Id, Name
            FROM Account 
            LIMIT 10
        ];
    }
}
0

typically, you will extend your current server side controller to have an update method.

you would pass the update values in via the client side controller in the lightning component.

detail is here:

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/controllers_server_actions_call.htm

where i've spent time debugging this in the past, is forgetting to add the $A.enqueueAction(action) call at the end of the callback.. :)

2

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.