sercrod

:action

Summary

:action is an attribute binding directive that controls the action attribute of a form element. It evaluates a Sercrod expression and writes the result into the form action attribute, optionally passing the value through a configurable url filter. The binding is one way: data updates change the DOM attribute, but changes to the DOM attribute do not update data.

:action is part of the generic colon attribute family and shares the same evaluation rules as other colon bindings such as :href and :src.

Basic example

A simple dynamic form endpoint:

<serc-rod id="app" data='{
  "endpoint": "/api/contact"
}'>
  <form method="post" :action="endpoint">
    <label>
      Name:
      <input type="text" name="name">
    </label>
    <button type="submit">Send</button>
  </form>
</serc-rod>

Behavior:

Behavior

Core rules:

Evaluation timing

:action is evaluated during the element rendering phase, together with other colon style attribute bindings.

More precisely:

There is no separate scheduling or delay specific to :action. It participates in the normal render update cycle of the host.

Execution model

Conceptually, when Sercrod renders an element with :action, the steps are:

  1. Sercrod encounters a node that has an attribute named :action.

  2. It reads the expression string from that attribute, for example endpoint or isEdit ? editUrl : createUrl.

  3. It evaluates the expression with eval_expr, using the current scope and a context that sets mode to attr:action and el to the current element.

  4. It inspects the result:

    • If the result is strictly false, or is null or undefined, Sercrod removes the action attribute from the element and stops.
    • Otherwise, it converts the result to a string and passes it to the url filter, along with the attribute name "action" and a context with the element and scope.
  5. If the url filter returns a truthy string, Sercrod sets the element attribute action to that string.

  6. If evaluation throws an exception at any point, Sercrod falls back to setting action to an empty string.

  7. If cleanup.handlers is enabled in the configuration, Sercrod removes the original :action attribute from the element, leaving only the concrete action attribute in the output DOM.

Use with structural directives and loops

Because :action is only an attribute binding, it composes freely with structural directives around it.

Typical combinations:

There are no special restrictions on combining :action with structural directives such as *if, *for, or *each, because those operate at the element or container level, while :action only binds an attribute.

Best practices

Additional examples

Dynamic endpoint based on mode:

<serc-rod id="app" data='{
  "mode": "create",
  "createEndpoint": "/api/users/create",
  "editEndpoint": "/api/users/edit"
}'>
  <form method="post"
        :action="mode === 'edit' ? editEndpoint : createEndpoint">
    <!-- fields -->
  </form>
</serc-rod>

When mode is "create", the form posts to /api/users/create. When mode switches to "edit" and the host updates, :action changes the action attribute to /api/users/edit.

Switching between environments:

<serc-rod id="app" data='{
  "env": "staging",
  "endpoints": {
    "staging": "https://staging.example.com/api/submit",
    "production": "https://example.com/api/submit"
  }
}'>
  <form method="post" :action="endpoints[env]">
    <!-- fields -->
  </form>
</serc-rod>

The same template can be reused across staging and production by changing env in data, without touching the markup.

Notes