View Source Backpex.ResourceAction behaviour (Backpex v0.9.1)
Behaviour implemented by all resource action modules.
use Backpex.ResourceAction
When you use Backpex.ResourceAction
, the Backpex.ResourceAction
module will set @behavior Backpex.ResourceAction
.
In addition it will implement the base_schema/1
function in order to generate a schemaless changeset by default.
Summary
Callbacks
The base item / schema to use for the changeset. The result will be passed as the first parameter to changeset/3
each time it is called.
The changeset to be used in the resource action. It may be used to validate form inputs.
A list of fields to be displayed in the resource action. See Backpex.Field
. In addition you have to provide
a type
for each field in order to support changeset generation.
Performs the action. It takes the socket and the casted and validated data (received from Ecto.Changeset.apply_action/2
).
The label of the resource action. It will be the label for the resource action button.
The title of the resource action. It will be part of the page header and slide over title.
Callbacks
@callback base_schema(assigns :: map()) :: Ecto.Schema.t() | Ecto.Changeset.t() | {Ecto.Changeset.data(), Ecto.Changeset.types()}
The base item / schema to use for the changeset. The result will be passed as the first parameter to changeset/3
each time it is called.
This function is optional and can be used to use changesets with schemas in item actions. If this function is not provided,
a schemaless changeset will be created with the provided types from fields/0
.
@callback changeset( change :: Ecto.Schema.t() | Ecto.Changeset.t() | {Ecto.Changeset.data(), Ecto.Changeset.types()}, attrs :: map(), metadata :: keyword() ) :: Ecto.Changeset.t()
The changeset to be used in the resource action. It may be used to validate form inputs.
Additional metadata is passed as a keyword list via the third parameter.
The list of metadata:
:assigns
- the assigns:target
- the name of theform
target that triggered the changeset call. Default tonil
if the call was not triggered by a form field.
@callback fields() :: list()
A list of fields to be displayed in the resource action. See Backpex.Field
. In addition you have to provide
a type
for each field in order to support changeset generation.
@callback handle(socket :: Phoenix.LiveView.Socket.t(), data :: map()) :: {:ok, Phoenix.LiveView.Socket.t()} | {:error, Ecto.Changeset.t()}
Performs the action. It takes the socket and the casted and validated data (received from Ecto.Changeset.apply_action/2
).
You must return either {:ok, socket}
or {:error, changeset}
.
If {:ok, socket}
is returned, the action is considered successful by Backpex and the action modal is closed. However, you can add an error flash message to the socket to indicate that something has gone wrong.
If {:error, changeset}
is returned, the changeset is used to update the form to display the errors. Note that Backpex already validates the form for you. Therefore it is only necessary in rare cases to perform additional validation and return a changeset from handle/2
.
You have to use Phoenix.LiveView.put_flash/3
along with the socket to show a success or error message.
@callback label() :: binary()
The label of the resource action. It will be the label for the resource action button.
@callback title() :: binary()
The title of the resource action. It will be part of the page header and slide over title.
Functions
Defines Backpex.ResourceAction
behaviour.
Gets the name of a resource action.