10

Digging in deeper to Javascript Remoting and I'm a bit confused. The Salesforce Documentation starts off by showing how to set up everything and uses code like the following:

function getRemoteAccount() {
    var accountName = document.getElementById('acctSearch').value;

    // This remoting call will use the page's timeout value
    Visualforce.remoting.Manager.invokeAction(
        '{!$RemoteAction.AccountRemoter.getAccount}',
        accountName, 
        handleResult
    );
}

However, I've also seen examples where Visualforce.remoting.Manager.invokeAction is never called. They end up looking something like the following:

function getText() {
  var text = "";
  JSRemotingBasicsController.doGetText(
    function(result, event) {
      // do something here
    }
  );
}

I'm wondering what is the point to using Visualforce.remoting.Manager.invokeAction when you can just reference the RemoteAction directly like the second example? Is there something I'm missing? Is there a time where the second example doesn't work?

1 Answer 1

7

The main difference that I know of is that the invokeAction call will automatically resolve namespaces for you.

For example, if your remote method is in a class within a namespace the second method would have to be called as follows:

namespace.controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);

When using Visualforce.remoting.Manager.invokeAction you can use $RemoteAction to automatically resolve the namespace and give you the fully qualified remote action name.

So the example above would become the following (notice the namespace is omitted):

Visualforce.remoting.Manager.invokeAction(
    '{!$RemoteAction.controller.method}', 
    [parameters...,]
    callbackFunction,
    [configuration]
);

One thing to keep in mind with this method is that if $RemoteAction finds matching @RemoteAction methods in multiple namespaces, it returns the first matching method and logs a warning to the JavaScript console.

1
  • Thanks for confirming my initial thought of not too much difference. Namespaces could be an issue, but I prefer to be more explicit than implicit in those cases anyway.
    – ricka
    Commented Oct 17, 2014 at 20:39

You must log in to answer this question.

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