sercrod

@blur

Summary

@blur attaches a handler to the native blur event on an element. The expression on @blur is evaluated when the element loses focus. Typical uses include marking a field as “touched”, running lightweight validation, or committing staged values when the user leaves a control.

@blur is part of the event handler family (such as @click, @input, @change, @focus) and follows the same general evaluation rules as other @ directives.

Basic example

Marking an input as “touched” when it loses focus:

<serc-rod id="app" data='{
  "profile": { "name": "" },
  "touched": { "name": false }
}'>
  <label>
    Name:
    <input type="text"
           :value="profile.name"
           @blur="touched.name = true">
  </label>

  <p *if="touched.name && !profile.name">
    Please enter your name.
  </p>
</serc-rod>

Behavior:

Behavior

Core rules:

Sercrod does not change the native timing or semantics of the blur event; it only injects expression evaluation when the event occurs.

Evaluation timing

@blur participates in Sercrod’s normal event lifecycle:

There is no special debounce or scheduling around @blur; it runs synchronously when the event fires, in the same turn as the native blur event.

Execution model

Conceptually, the runtime behaves as follows for @blur:

  1. During rendering, Sercrod finds an element with an attribute named @blur.
  2. It reads the attribute value as a source string, for example touched.name = true or validateField('name').
  3. Sercrod compiles or stores this expression so it can be evaluated later in the current host’s scope.
  4. Sercrod attaches a native event listener for "blur" on that element.
  5. When the browser fires blur:
    • Sercrod prepares the evaluation context for this host and element.
    • Sercrod evaluates the stored expression with its expression evaluator.
    • If evaluation throws, Sercrod catches the error and logs it through the configured logging mechanism; the error does not stop other handlers or other elements from updating.
  6. If cleanup.handlers is enabled in the configuration, Sercrod may remove the original @blur attribute from the DOM, leaving only the wired listener and any other visible attributes.

The listener itself is lean: it does not try to change the propagation of the event (such as stopping it) unless your expression does so through normal browser APIs.

Use with form fields and data bindings

@blur is often paired with data bindings to form controls:

Because @blur runs only when focus leaves the element, it is a good fit for:

Use with conditionals and loops

@blur can be freely combined with structural directives that control the element itself:

If a structural directive replaces the element on re-render (for example via key changes or switching a block), Sercrod re-attaches @blur on the new element instance as part of its normal rendering process.

Sercrod-specific restrictions

For @blur, there are no special Sercrod-only restrictions beyond the general event rules:

Unlike structural directives (*each, *include, *import), event directives do not compete for ownership of the element’s children, so there is no “only one of these” constraint specific to @blur.

Best practices

Additional examples

Mark field as dirty only when value actually changed:

<serc-rod id="app" data='{
  "original": { "name": "Taro" },
  "current":  { "name": "Taro" },
  "dirty":    { "name": false }
}'>
  <input type="text"
         :value="current.name"
         @blur="dirty.name = (current.name !== original.name)">
</serc-rod>

Local validation on blur:

<serc-rod id="app" data='{
  "email": "",
  "errors": { "email": "" }
}'>
  <input type="email"
         :value="email"
         @blur="
           errors.email =
             (!email || email.indexOf('@') === -1)
               ? 'Please enter a valid email.'
               : '';
         ">

  <p *if="errors.email" *print="errors.email"></p>
</serc-rod>

Toggle helper text visibility:

<serc-rod id="app" data='{
  "show_help": true
}'>
  <input type="text"
         :value="''"
         @blur="show_help = false">

  <p *if="show_help">
    You can leave this field empty.
  </p>
</serc-rod>

Notes