function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sachin Bhalerao 17Sachin Bhalerao 17 

Apex class is acts as a controller and Model in Lightning component ?

Dear Team ,

Trust you are doing fine !!!

Please let me know Apex acts as a model or controller in Lightning Component .  One more thing through this diagram it is showing controller.js will call APEX class but in Lightning component file we are calling Controller in this way

<aura:component controller="AccountController">

plz let me know exactly the scenario 

User-added image

Thanks & Regards
Sachin Bhalerao
Best Answer chosen by Sachin Bhalerao 17
Ajay K DubediAjay K Dubedi
Hi Sachin,

In the MVC architecture, Apex Class Acts as a controller.
The diagram that you share with us shows that controller.js will call the apex class.
Now how all this work, In Lightning page you have both controller.cmp
and controller.js, you call apex class methods from controller.js and to show that data in Controller.cmp
you use <aura:component controller="AccountController">

Here is an example for you,
 
Inline_Account_Page.cmp
----------------------------
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="Inline_Account_Class">
<aura:attribute name="account1" type="Account1__c[]"/>
<aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
<table class="slds-table slds-table_bordered slds-table_cell-buffer">
    <div class="scrollable">
        <h1 align="center"><b><u>Account1</u></b></h1><br/>
        <table>
            <thead>
                <th>NAME</th>
            </thead>
            <aura:iteration items="{!v.account1}" var="ac1" >
                <tr>
                    <td>
                        {!ac1.Name}
                    </td>
                </tr>
            </aura:iteration>
        </table>
    </div>
</table>
</aura:component>

Inline_Account_Page.js
-----------------------------
myAction : function(component, event, helper) 
{
var account_id = component.get("v.recordId");
var action = component.get("c.Inline_Account_Class_Method");
action.setParams({
              Ac1_Id : account_id
             });
action.setCallback(this,function(a)
               {
                   component.set("v.account1",a.getReturnValue());
               });
$A.enqueueAction(action);
},

Apex Class
-------------------------
public class Inline_Account_Class 
{
@AuraEnabled
public static List<Account1__c> Inline_Account_Class_Method(ID Ac1_Id)
{
List<Account1__c> Account1_List = [select id,Name,ParentId__c from Account1__c where ParentId__c=: Ac1_Id];
return Account1_List;
}
}

Now you see that Apex class Methods are called in 'Inline_Account_Page.js' and you can't show that in 'Inline_Account_Page.cmp' as it shows an error if you are trying to run it You need to access the class name in 'Inline_Account_Page.cmp'


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi Sachin,


Greetings to you!

I have gone through your query and I realized.

In lightning component MVC architecture an Apex class act as a controller not model. 
because there are two controller server side controller and client side controller, Apex controller is a server side controller and Controller.js is client side controller.

<aura:component controller="AccountController">

In this case, we use controller = "AccountController" to handle server-side operation. without use controller in <aura: component> we can not call Apex class method through controller.js.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Sachin Bhalerao 17Sachin Bhalerao 17
Hi Deepali,

Thank you for your reply now m clear about APEX Class . One more thing according to diagram it is mentioned that client side controller is interacting with apex class but we are calling apex class using <aura:component controller="AccountController"> this syntax  means we are not using client side controller to interact with APEX class.

Please let me know this diagram is incorrect or i am not getting exact meaning of diagram .

Thanks & Regards
Sachin Bhalerao
Deepali KulshresthaDeepali Kulshrestha
Hi Sachin,


<aura:component controller="AccountController">

In this case, we are using a client-side controller to interact with the APEX class because the apex class method can not call directly through aura component.

For example:

Aura Component
------------------
<aura:component controller="AccountController">
<aura:attribute name="acclist" type="Account[]"/>
<aura:handler name="init" value="{!this}" action="{!c.allAccount}"/>
<table aria-multiselectable="true" class="slds-table slds-table_bordered slds-table_fixed-layout slds-table_resizable-cols" role="grid">
        <thead>
           <tr class="slds-line-height_reset">
                <th class="" scope="col">
                    <div class="slds-truncate" title="Account Name">ACCOUNT NAME</div>
                </th>
            </tr>
        </thead>
        <tbody>           
            <aura:iteration items="{!v.acclist}" var="acc">
                <tr>
                    <td>
                        {!acc.Name}
                    </td>
    </table>
</aura:component>

Javascript controller
------------------------
({
    allAccount : function(component, event, helper) {
        var action = component.get("c.getAllAccounts");
        
        action.setCallback(this,function(a)
                           {
                               component.set("v.acclist",a.getReturnValue()) ; 
                           });
        
        $A.enqueueAction(action);
    }
    
})

Apex class
---------------------
public class AccountController {

    @AuraEnabled
    public static List<Account> getAllAccounts()
    {
        List<account> acc_list =[select Id,Name from account limit 100];
        return acc_list; 
    }    
}


This diagram is correct. 

I hope you find the above solution helpful.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
Ajay K DubediAjay K Dubedi
Hi Sachin,

In the MVC architecture, Apex Class Acts as a controller.
The diagram that you share with us shows that controller.js will call the apex class.
Now how all this work, In Lightning page you have both controller.cmp
and controller.js, you call apex class methods from controller.js and to show that data in Controller.cmp
you use <aura:component controller="AccountController">

Here is an example for you,
 
Inline_Account_Page.cmp
----------------------------
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="Inline_Account_Class">
<aura:attribute name="account1" type="Account1__c[]"/>
<aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
<table class="slds-table slds-table_bordered slds-table_cell-buffer">
    <div class="scrollable">
        <h1 align="center"><b><u>Account1</u></b></h1><br/>
        <table>
            <thead>
                <th>NAME</th>
            </thead>
            <aura:iteration items="{!v.account1}" var="ac1" >
                <tr>
                    <td>
                        {!ac1.Name}
                    </td>
                </tr>
            </aura:iteration>
        </table>
    </div>
</table>
</aura:component>

Inline_Account_Page.js
-----------------------------
myAction : function(component, event, helper) 
{
var account_id = component.get("v.recordId");
var action = component.get("c.Inline_Account_Class_Method");
action.setParams({
              Ac1_Id : account_id
             });
action.setCallback(this,function(a)
               {
                   component.set("v.account1",a.getReturnValue());
               });
$A.enqueueAction(action);
},

Apex Class
-------------------------
public class Inline_Account_Class 
{
@AuraEnabled
public static List<Account1__c> Inline_Account_Class_Method(ID Ac1_Id)
{
List<Account1__c> Account1_List = [select id,Name,ParentId__c from Account1__c where ParentId__c=: Ac1_Id];
return Account1_List;
}
}

Now you see that Apex class Methods are called in 'Inline_Account_Page.js' and you can't show that in 'Inline_Account_Page.cmp' as it shows an error if you are trying to run it You need to access the class name in 'Inline_Account_Page.cmp'


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer