Can someone tell me the difference between these VF tags... in brief?
3 Answers
@RemoteAction
and apex:actionFunction
are essentially polar opposites in terms of functionality, even though they both offer JavaScript functionality.
@RemoteAction
methods are static, and therefore can't see the current page state directly, while apex:actionFunction
methods are instance methods, and so can see the entire page state.
@RemoteAction
methods require less bandwidth, and server processing time, because only the data you submit is visible and the view state is not transferred, while apex:actionFunction
has to transfer the page view state.
@RemoteAction
methods have to update the DOM manually using explicit JavaScript, while apex:actionFunction
methods automatically update the Visualforce DOM and can refresh part or all of the page, and can provide a standard interface for showing a loading status through apex:actionStatus
.
@RemoteAction
methods can return data directly back to the calling JavaScript, but cannot update the page's view state. apex:actionFunction
methods can update the page's view state and DOM structure, but cannot return data directly back to JavaScript (although you can do this with some extra effort using oncomplete
).
Because of these polar differences, there is almost always "one right answer" for which method should be used in a given situation. An auto-complete-as-you-type feature would benefit from @RemoteAction
, because less data is used, and so it is faster and more responsive; we don't need to modify the view state, so this is a major bonus for the user. Updating the page's view state to show new Visualforce elements, in contrast, would best be served with apex:actionFunction
; using a @RemoteAction
would force the developer to manage the DOM themselves, which can be more burdensome.
Also, you generally can't modify the DOM in native JavaScript if you use apex:actionFunction
, because those changes won't appear in the view state and will be overwritten if the DOM node the changes appeared in is refreshed. This means that JavaScript designed to use @RemoteAction
and update the DOM as a result will generally be incompatible with apex:actionFunction
. There's exceptions to this rule, of course, as long as both uses are isolated from each other or interact only in a carefully designed order of operations.
-
An
apex:actionFunction
can rerender a script tag containing merge fields which will be reflected in the view state and can be used to update the DOM. Sep 27, 2013 at 14:33 -
1
@RemoteAction
is an Apex annotation used to mark a method as being available for javascript remoting. This allows you to call the method from javascript yourself and retrieve the result as a javascript object for manipulation, as per the example in the documentation. They are static and global, and hence don't have access to your current controller variables and methods.
<apex:actionFunction>
is a Visualforce tag which has been around longer than remoting and allows you to invoke a controller action from javascript; it doesn't let you get retrieve data but you can rerender the page or a specific section of the page to update it with new values from the controller instance.