sercrod

:href

Summary

:href is an attribute binding directive that controls the href attribute of a link or resource element. It evaluates a Sercrod expression and writes the result into the href 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.

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

Basic example

A simple data driven link:

<serc-rod id="app" data='{
  "url": "https://example.com/docs"
}'>
  <a :href="url">Open documentation</a>
</serc-rod>

Behavior:

Behavior

Core rules:

Evaluation timing

:href 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 :href. It participates in the normal render update cycle of the host.

Execution model

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

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

  2. It reads the expression string from that attribute, for example url or base + "/users/" + userId.

  3. It evaluates the expression with eval_expr, using the current scope and a context that sets mode to attr:href 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 href 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 "href" and a context with the element and scope.
  5. If the url filter returns a truthy string, Sercrod sets the element attribute href to that string.

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

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

Use with structural directives and loops

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

Typical combinations:

Best practices

Additional examples

Building URLs from parameters:

<serc-rod id="app" data='{
  "userId": 42,
  "base": "/users"
}'>
  <a :href="base + '/' + userId">View profile</a>
</serc-rod>

Localised links:

<serc-rod id="app" data='{
  "locale": "en",
  "docs": {
    "en": "/docs/en/guide",
    "ja": "/docs/ja/guide"
  }
}'>
  <a :href="docs[locale]">Read guide</a>
</serc-rod>

Links that can be disabled:

<serc-rod id="app" data='{
  "item": { "url": "/pay", "enabled": false }
}'>
  <a :href="item.enabled ? item.url : null"
     @click="item.enabled && pay(item)">
    Proceed to payment
  </a>
</serc-rod>

When item.enabled is false, :href removes the href attribute, and only the click handler (if not guarded) will run.

Notes