sercrod

*save

Summary

*save exports the host data (or its staged view) as a JSON file in the browser. It is typically used on a button inside a <serc-rod> host. When clicked, *save collects the host’s current data, builds a JSON string, and starts a download such as Sercrod-20251205-093000.json.

By default, *save exports the entire host data. If you provide a list of property names, only those top-level properties are included.

Basic example

Save the entire host data:

<serc-rod id="profile" data='{"name":"Alice","age":30}'>
  <button *save>Download profile JSON</button>
</serc-rod>

Behavior:

Behavior

Alias:

Data source and property selection

Data source:

Property selection:

Example (selective save):

<serc-rod id="settings" data='{
  "user": { "name": "Alice", "age": 30 },
  "theme": { "mode": "dark" },
  "debug": true
}'>
  <!-- Only save "user" and "theme" from the host data -->
  <button *save="user theme">Download user+theme</button>
</serc-rod>

In this example:

Evaluation timing

Render-time:

Click-time:

Because the JSON is built at click time, *save always reflects the current state of _stage or _data at the moment of the click.

Execution model

Internally, *save behaves as follows (conceptually):

  1. During render, Sercrod finds an element work with *save or n-save.

  2. Sercrod clones work into el.

  3. Sercrod attaches:

    • el.addEventListener("click", () => { /* build JSON and download */ }).
  4. Sercrod appends el to the parent node and returns, without processing el’s children for further Sercrod directives.

On click, the handler:

  1. Reads the attribute value to obtain a property list (or null if empty).

  2. Selects src:

    • src = host._stage ?? host._data.
  3. Builds data:

    • If there is a property list, data is a new object populated only with properties present in src.
    • Otherwise, data is src itself.
  4. Serializes data with a pretty-printed JSON.stringify(data, null, 2).

  5. Creates a Blob of type application/json.

  6. Creates an ObjectURL and a temporary <a> element with:

    • href = url.
    • download = "Sercrod-YYYYMMDD-HHMMSS.json" (in the local time of the browser).
  7. Programmatically clicks the anchor to prompt download.

  8. Cleans up (removes the anchor from the DOM and revokes the ObjectURL).

  9. Dispatches CustomEvent("sercrod-saved", { detail: { ... } }) from the host.

Use on nested elements and scope

In other words:

Events

After starting the download, *save dispatches a bubbling, composed CustomEvent from the host:

Example hook:

document.addEventListener("sercrod-saved", (evt) => {
  const { host, fileName, props, json } = evt.detail;
  console.log("Saved from host:", host.id);
  console.log("File name:", fileName);
  console.log("Props:", props);
  console.log("JSON preview:", json.slice(0, 200));
});

You can use this event to:

Best practices

Advanced - Using *save with *stage, *apply, *restore, *load, and *post

*save is part of a broader data management workflow:

The core rule is:

Notes