Actions
The Actions allow you to run server code directly from a GOX page.
Unlike HTTP request methods, an Action can be invoked from JavaScript code without defining additional routes or implementing an API manually.
GOX automatically generates communication between the browser and the server, allowing the Actions to be used as if they were asynchronous JavaScript functions.
Declaration and invocation
An Action is declared as a return function gox.Action.
func Greeting(name string) gox.Action {
return "Hello, " + name
} The function name identifies the Action that will be available for the page.
The Actions are invoked from JavaScript using the module @gox/actions.
import Actions from "@gox/actions"
const [message, err] = await Actions.Greeting("George")
if (err) {
console.error(err.message)
return
}
console.log(message) It is not necessary to create HTTP routes, additional endpoints or custom client.
Visibility
The Actions always belong to a page.
Each page completely defines the Actions API available for its own JavaScript code. This means that an Action can only be invoked from the page it belongs to and is not part of a global project registry.
For example, if the page /dashboard declares the following Action:
func RefreshDashboard() gox.Action {
...
} may be invoked from the JavaScript code of that page.
However, that same Action will not be automatically available from another page, such as /users.
This behavior allows to keep the pages unconnected and ensures that each one exposes only the operations that are part of its own interface.
Modularization
The Actions do not need to be declared in the main file of a page. They can be freely distributed between different modules to better organize the application code.
During the compilation, GOX automatically collects the Actions declared by the page and its imported modules, incorporating them into the set of Actions available for that page. As a result, an Action can be invoked from JavaScript regardless of the file in which it was declared.
// dashboard.actions.gox
func RefreshDashboard() gox.Action {
...
} // index.gox
import "./dashboard.actions.gox" const [result, err] = await Actions.RefreshDashboard() This feature allows you to divide a page into multiple modules without changing the way the Actions are used from JavaScript.
Unique names
All available Actions for a page share the same name space.
For this reason, their names must be unique, regardless of the module where they are declared.
If two modules declare an Action with the same name, GOX stops the compilation and reports a duplication error.
// users.actions.gox
func Save() gox.Action // settings.actions.gox
func Save() gox.Action // index.gox
import (
"./users.actions.gox"
"./settings.actions.gox"
) In this case, both Actions would try to state the name Save for the same page, so GOX requests to resolve the conflict before continuing with the compilation.
Integration with Go
The Actions are built using conventional Go functions.
They do not require a special syntax or a mechanism different from that of their own language. It is enough to declare a function whose type of return is gox.Action.
This allows you to organize, reuse and modulate the code using the same tools that Go offers for the rest of the application.
Best practices
Group related actions in modules with a clear responsibility.
It uses descriptive names that represent the operation they perform and avoids too generic names when a page can grow over time.
Whenever possible, keep the Actions small and delegate business logic to specialized packages or modules.
To learn more about PagesSee your documentation. To learn more about Methods, check your documentation.