Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better workflow for adding single items to the page #438

Closed
g105b opened this issue Jul 18, 2023 · 1 comment
Closed

Better workflow for adding single items to the page #438

g105b opened this issue Jul 18, 2023 · 1 comment

Comments

@g105b
Copy link
Member

g105b commented Jul 18, 2023

I much prefer the terminology data-list when working with elements that represent a list of items that represent an iterable data structure.

However, when I want to add a single item to the page, such as adding an error message to a form, it made more sense calling it data-template... but the workflow for adding the element back to the page was a bit clunky.

As an example, this is what the code looks like to add an error message to a form:

<form>

  <div data-list="error" data-bind:text>Error message goes here</div>

  <label>
    <span>Your email</span>
    <input name="email" />
  <label>
  <label>
    <span>Your password</span>
    <input type="password" name="pass" />
  </label>

  <button name="do" value="login">Log in!</button>
</form>
function do_login(
  Input $input, // obviously required
  ListElementCollection $listElementCollection, // cumbersome name, and overly verbose to use
  DocumentBinder $binder, // we still need to manually bind
  HTMLDocument $document, // AND we need the document to pass in to the list element collection :(
):void {
  if(!isAuthenticated($input->getString("email", "pass")) {
    $el = $listElementCollection->get($document, "error");
    $inserted = $el->insertListItem();
    $binder->bindKeyValue("error", "Invalid authentication details", $inserted);
  }
}

This calls for a new type of templated element, because that code above is stupidly verbose.

Here's my initial idea:

<form>
  <div data-remove-unbound data-bind:text="error">Error message goes here</div>

  <label>
    <span>Your email</span>
    <input name="email" />
  <label>
  <label>
    <span>Your password</span>
    <input type="password" name="pass" />
  </label>

  <button name="do" value="login">Log in!</button>
</form>

The data-remove-unbound attribute will remove the element if it doesn't get anything bound to it; that is, if there isn't a call to $binder->bindKeyValue("error", "something");

So the PHP would look like this:

function do_login(
  Input $input, // obviously required
  DocumentBinder $binder, // we now only need the binder
):void {
  if(!isAuthenticated($input->getString("email", "pass")) {
// only one line required to keep the element in the document:
    $binder->bindKeyValue("error", "Invalid authentication details");
  }
}
@g105b
Copy link
Member Author

g105b commented Jul 18, 2023

Linked PR: #439, but I'm deciding whether to switch to a different attribute name before it's merged.

g105b added a commit that referenced this issue Jul 19, 2023
@g105b g105b closed this as completed in ad208ed Jul 19, 2023
g105b added a commit that referenced this issue Jul 19, 2023
* build: upgrade dom requirement and loosen version range

* docs: update examples

* feature: trim whitespace when there are only template children
closes #363

* maintenance: phpstorm analysis improvements

* feature: remove unbound elements that are marked accordingly
closes #438

* test: ensure element does not get removed after bound
for #438

* feature: use `data-element` attribute for #438

* tweak: remove data-element attribute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant