4

I have a controller with the below (sanitized) methods:

@remoteAction
global static String getParam1(String someInput1){
    String param1 = someInput1;
    return param1;
}

@remoteAction
global static String getParam2(String someInput2){
    String param2 = someInput2;
    return param2;
}

And a VF page with the following javascript:

<script>

//vf remoting
Visualforce.remoting.Manager.invokeAction("{!$RemoteAction.controller.getParam1}", aninputhere, function(result,event){
var jsParam1 = result;});

//calling method directly
controller.getParam2(aninputhere,function(result,event){
    var jsParam2 = result;});

</script>

While I have done this inconsistently by accidental both work fine in all contexts that they are used...is there any benefit/reason I should use the vf remoting approach in javascript over calling the controller directly?

2 Answers 2

2

Just verbosity. I prefer the latter because it is more concise. As far as I know, that's the only difference. As a direct consequence, it is also somewhat easier to pass the reference to a static resource/angular framework/etc, since you can pass a single function instead of the entire manager.

As a simple example, you could add that specific function to a namespace:

(function (w) {
    "use strict";
    w.myNamespace = w.myNamespace || {};
    w.myNamespace.callRemoteAction = controller.getParam2;
    // other logic, cleanup, etc.
}(window));
5
  • Makes sense and I did see reference to namespace in the docs: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/…
    – Girbot
    Aug 25, 2016 at 19:00
  • @Girbot I'm using it in a different sense here. More like this.
    – Adrian Larson
    Aug 25, 2016 at 19:04
  • @AdrianLarson With namespaces the latter approach has created issues in the past .Is this fixed now ? Do you package your code ? Aug 25, 2016 at 20:26
  • 1
    @MohithShrivastava Nope, I've never packaged anything. Interesting. It seems like you should be able to work around that limitation, but I'll trust you that for managed code it's simpler to invokeAction. I still hold that it's easier to incorporate the above into SPA architecture though.
    – Adrian Larson
    Aug 25, 2016 at 20:28
  • 1
    You can pass the global variable {!$RemoteAction.controller.getParam1} and use Visualforce.remoting.Manager.invokeAction(windowvariable) in the SPA . Aug 25, 2016 at 20:31
2

I will prefer the first approach of using $RemoteAction

Visualforce.remoting.Manager.invokeAction("{!$RemoteAction.controller.getParam1}", aninputhere, function(result,event){

The reason is this is namespace independent and I will not have to worry about namespace when i package the visualforce page for managed package application as the platform automatically takes care of namespacing .

You must log in to answer this question.

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