3

In the documentation I found the following tip:

Use unique names for client-side and server-side actions in a component. A JavaScript function (client-side action) with the same name as a server-side action (Apex method) can lead to hard-to-debug issues.

Out of curiosity I tried doing exactly this, creating a client/Javascript controller with the same name as a method on my server/APEX controller. When I did this and triggered the client controller's action, the callback seemed to get stuck in a repeating infinite loop (as though the action was being called over and over). It ran for several thousand iterations before it locked up my Chrome tab.

If I change ONLY the name of the server/APEX controller's method, everything works as expected, with the callback being executed exactly once.

I know the documentation's advice is to not name them similarly, but I'm curious of it's actually BROKEN (as opposed to merely "not advised").

Is having identical names merely not recommended? Or is it literally broken and not allowed? If it IS allowed, what am I doing wrong to cause this infinite loop?

Component

<aura:application controller="TestSameNames">
    <ui:button label="Go" press="{!c.someFunctionName}" />
</aura:application>

Controller (client/Javascript)

({
    someFunctionName : function(component, event, helper) {
        console.log("in the Client controller");
        
        var action=component.get("c.someOtherFunctionName");
        
        action.setCallback(this, function(response) {
            if (response.getState()==="SUCCESS") {
                console.log("IN CALLBACK");
            } 
        });
        
        $A.enqueueAction(action);
    }
})

Controller (server/APEX)

public class TestSameNames {

    @AuraEnabled
    public static String someOtherFunctionName() {

    /*  IF I RENAME THIS METHOD to "someFunctionName" (and 
        adjust the client controller's call above) it works fine  */

        return 'Hey';
    }
    
}
1

1 Answer 1

2

I think, it is just not recommended, because if you go further ahead and try the following in your example:

Component

<aura:component controller="MyController">
    <ui:button label="Test" press="{!c.start}" />
</aura:component>

JS Controller

 start : function(component, event, helper) {
    helper.start(component);
 },
 test : function(component, event, helper) {
    console.log('in Js controller method') ;
    return 'xyz'
 }

JS Helper

start : function(component, event, helper) {
        var action = component.get('c.test');
        action.setCallback(this, function(resp) {
            var state= resp.getState();
            console.log('IN CALLBACK; STATE='+state);
            console.log('IN CALLBACK; Response='+state);
        });

        $A.enqueueAction(action);
}

And do not modify anything in the apex class.Once you click on the button you can see the following in log :

in Js controller method

IN CALLBACK; STATE="SUCCESS"

IN CALLBACK; Response="xyz"

So, the code does not break and runs fine, but at the same time it doesn't behave as expected. component.get('c.test'); statement returns a reference to JS contorller's action 'test' instead of the apex action.

3
  • So if a server/APEX controller method has an identical name as a client/Javascript controller, then component.get('c.someMethodName') returns a reference to the client/Javascript's method? I thought that within the context of a client/Javascript controller, component.get('c.xyz') referred to methods on the APEX CONTROLLER (in fact, the Trailhead training supports this: goo.gl/j9gCZB, see the section titled "Calling Server-Side Controller Methods"). If the names match, is there no way to get a reference to the server's method?
    – loneboat
    Jun 24, 2016 at 19:38
  • Yeah, that is the current behavior when the names of server side and client side actions match, but i'm not really sure f that is bug or the actual behavior. Also there is no other way to get a reference to a server's method and that's why it is not recommended to name the client and server actions same.
    – Gopal Rao
    Jun 27, 2016 at 3:33
  • Excellent, thank you for the follow-up. It's confusing because the Trailhead tutorials advise against naming them the same, but the only reason they give is because it will "make debugging difficult". If naming them the same actually changes behavior (e.g. server methods are no longer accessible), I think they ought to explain that in their documentation. Thanks for your help!
    – loneboat
    Jun 27, 2016 at 4:04

You must log in to answer this question.

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