Mimicking callback functions for Visualforce ActionFuncitons
This article demonstrates a clever approach to mimic dynamic callback functions for asynchronous Visualforce actionFunctions, which normally support only a static oncomplete handler. It tackles the issue of multiple components wanting to share a single actionFunction but needing distinct callbacks upon completion. By creating a global JavaScript object to register and dispatch callbacks dynamically, Salesforce developers can enable more flexible, reusable patterns, including chaining of asynchronous calls. The solution also discusses handling concurrency challenges and presents a loading screen strategy to prevent callback overwrites.
- Use a global JS object to store dynamic callbacks keyed by actionFunction names.
- Dispatch callbacks from a shared oncomplete handler using the registered callbacks.
- Implement a loading overlay to prevent user actions that overwrite callbacks during async calls.
- Callback functions can be chained to execute sequential Visualforce actionFunctions.
- Avoid repetitive code by sharing actionFunctions with dynamic callbacks across components.
Hey everyone. So I’ve got a nifty ‘approach’ for you this time around. So let me give you a quick run down on what I was doing, the problem I encountered and how I decided to solve it using what I believe to be a somewhat novel approach. The deal is that I have been working on a fairly complicated ‘one page’ app for mobile devices. What I decided to do was have one parent visualforce page, and a number of components that are hidden and shown depending on what ‘page’ the user is on. This allows for a global javascript scope to be shared between the components and also for them to have their own unique namespaces as well. I may cover the pros and cons of this architecture later. The issue I started to have, is that I wanted some action functions on the main parent container page to be used by the components in the page. That’s fine, no problem there.