1

I'm new to Aura. A tutorial I'm following shows that adding the Apex class to the controller attribute in the aura:component element is how you can run Apex. However, when I change the controller attribute from the JS controller to the Apex controller, the JS controller stops working. Is this expected? How can I keep the JS controller referenced while also running Apex?

Original JS controller referenced:

<aura:component
  implements="flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,force:hasRecordId"
  controller="TestController"
  access="global"
>

Apex controller referenced:

<aura:component
  implements="flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,force:hasRecordId"
  controller="TestApexController"
  access="global"
>

3 Answers 3

2

When you use <aura:component controller="SomeApexClass">, all @AuraEnabled methods in that Apex class are imported into your controller. That means if you have the following Apex:

@AuraEnabled public static Data init() {
...

And:

({
  init: function(component, event, helper) {
  ...

These two methods will be in conflict, and you'll have problems calling the methods or wiring them to the markup. Make sure all of the function names are unique.

1

The controller attribute is for Apex controllers only. You do not use the controller attribute to declare a JavaScript controller.

The JavaScript controller should be automatically wired to your component: the platform knows they go together because they are part of the component bundle and follow a specific naming pattern (<componentName>Controller.js).

2
  • That's what's confusing David. When I reference the Apex controller in the 'controller' attribute, the JS controller stops being referenced/stops working. I'm positive that the JS controller is a part of the component's bundle.
    – T. Young
    May 1, 2021 at 19:42
  • The same result occurs when removing the explicit 'controller' statement completely. So there is no implicit connection for some reason.
    – T. Young
    May 1, 2021 at 19:50
0

The Apex controller has to be referred in Aura component file.

Syntax:

<aura:component implements="flexipage:availableForRecordHome,force:lightningQuickActionWithoutHeader,force:hasRecordId" controller="TestApexController" access="global">
    <!-- Optional component attributes here -->
    <!-- Optional HTML markup -->
    <div class="container"> 
        Hello world!
        <!-- Other components -->
    </div>
</aura:component>

In order to invoke methods in server side from controller.js or helper.js file

Syntax:

var action = component.get("c.ApexMethod");
action.setCallback(this, function(response){
    var state = response.getState();
    if(state == 'SUCCESS') {
        alert(response.getReturnValue());
    }
});
$A.enqueueAction(action);

You must log in to answer this question.

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