Category: JS Objects
Resource links
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Storing Functions in Appsmith Store with Callbacks

Issue

As a developer, I need to be able to assign event callbacks to dynamically created buttons in code, so that I can make my tables editable and have corresponding callbacks. How can this be achieved programmatically?

Resolution

Create a JS Object named MyJsObject with the following code snippet:

export default {
  myFunc1: () => { 
    showAlert('Ran myFunc1')
    return true
  }
}

Elsewhere in your JS, you can use the following line to store the function to the Appsmith Store: await storeValue('myWellNamedCallback', MyJsObject['myFunc1'])

In your button's onClick Event, you can use {{ appsmith.store.myWellNamedCallback?.() || myFallbackFunc() }}

You want any callback you define in your object to return true, so that we can use the OR operator (||) to define a fallback in case we haven’t stored a function yet.