Skip to content

[dasc] Keep form model JSON-only, HTML only at fetch and save boundaries #188

@kozmaadrian

Description

@kozmaadrian

Summary

  • The form model should not know about HTML, and it should not handle the save mechanism.
  • The fetch layer does HTML → JSON and passes only JSON into the form.
  • The model holds and mutates only JSON.
  • Persistence is the caller’s responsibility: the caller takes the model’s JSON, converts to HTML, and POSTs.
  • HTML appears only when fetching and when saving. Converting JSON to HTML on every field update is unnecessary; HTML is needed only for persisting.

Current behavior

sequenceDiagram
  participant FB as Form block
  participant Load as loadHtml
  participant FM as FormModel
  participant Server

  FB->>Load: fetch doc
  Load-->>FB: html
  FB->>FM: new FormModel(html)
  Note over FM: HTML→JSON, store _html + _json
  FB->>FM: updateProperty(name, value)
  Note over FM: setValueByPath(_json) then updateHtml() → _html
  FB->>FM: saveHtml()
  FM->>Server: POST(_html)
Loading
  • FormModel holds both _html and _json, and owns the save mechanism (saveHtml(), building the request, POSTing).
  • On every field edit, updateProperty() writes to _json then calls updateHtml() to reconvert _json_html and keep them in sync—even though _html is only used when saving.
  • Conversion and persistence both live inside the model; neither should be the model’s responsibility.

Proposed change

sequenceDiagram
  participant FB as Form block
  participant Load as loadDocument
  participant FM as FormModel
  participant Server

  FB->>Load: fetch doc
  Note over Load: fetch HTML, HTML→JSON
  Load-->>FB: { json }
  FB->>FM: new FormModel(json)
  Note over FM: store _json only
  FB->>FM: updateProperty(name, value)
  Note over FM: setValueByPath(_json) only
  FB->>FM: formModel.json
  FM-->>FB: json
  Note over FB: JSON→HTML
  FB->>Server: POST(html)
Loading
  • FormModel accepts only { path, json, schemas }; single source of truth _json.
  • The model does not handle save; the caller (form block or save helper) is responsible for persistence.
  • HTML conversion happens only at fetch and at save, outside the model.

Benefits

  • Separation of concerns: format (HTML) at the edges, form logic on JSON only; persistence is the caller’s responsibility, not the model’s.
  • Simpler model: one source of truth, no save logic; easier testing; clearer data flow (fetch → JSON → form → JSON → save).
  • Easier for new developers to understand the code: model = data only; fetch/save own format and persistence.
  • No JSON → HTML conversion on every field update; HTML is produced only when persisting.

Implementation (minimal)

  1. Fetch layer: Convert HTML → JSON and return { json }; form block uses that to construct the model.
  2. Form model: Drop html input, _html, converters, updateHtml() / updateJson(), html getter/setter; keep only JSON and updateProperty() on _json.
  3. Save: Move serialize-and-POST to form block or helper (caller owns persistence); remove saveHtml() from the model.
  4. Call sites: Update form block and any code using formModel.html or clone-with-HTML to use JSON only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    dascUsed by DA Structured Content

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions