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

Issue with built-in template repeat #179

Closed
klebba opened this issue Apr 26, 2024 · 4 comments · Fixed by #183
Closed

Issue with built-in template repeat #179

klebba opened this issue Apr 26, 2024 · 4 comments · Fixed by #183
Assignees

Comments

@klebba
Copy link
Collaborator

klebba commented Apr 26, 2024

TODO: needs a working repro case.

What's wrong:

The repeater does not re-render when data changes, only when the list of nodes is updated:

${repeat(nodes, node => node.id, node => html`
  <my-node
    id="${node.id}"
    .metadata="${node.metadata}"
    .data="${data[node.id]}">
  </my-node>
`)}

Workaround:

Coalesce nodes with data into a new list to feed the repeater (ensure new list reference is always generated)

Using version 1.0

@klebba
Copy link
Collaborator Author

klebba commented Apr 26, 2024

FYI @theengineear I'll try to build a working example eventually, just bookmarking this for later

@klebba
Copy link
Collaborator Author

klebba commented Aug 9, 2024

👋🏻 @Kelketek thank you for taking up this issue in #183. Just wanted to share an FYI that we recognize there are no contributing guidelines for you to follow and opened #186 to document a few thoughts. As you are the first notable community contributor to the project your feedback is welcome on the guidelines as well. Thanks!

@Kelketek
Copy link
Contributor

Thanks, @klebba ! I'll take a look :)

@klebba
Copy link
Collaborator Author

klebba commented Oct 7, 2024

Repro case:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>demo #179 issue</title>
  </head>
  <body>
    <demo-179></demo-179>
    <script type="module">
    import XElement from "https://deno.land/x/[email protected]/x-element.js";

    class Demo179 extends XElement {
      static get properties()	{
        return {
          items: {
            type: Array,
            default: () => [
              { id: 'a' },
              { id: 'b' },
              { id: 'c' }
            ],
          },
          lookup: {
            type: Object,
            default: () => {
              return {
                a: 'foo',
                b: 'bar',
                c: 'baz',
              };
            }
          },
        }
      }

      static template(html, { map }) {
        return ({ items, lookup }) => {
          return html`
            <div>
              <ul>
              ${
              	// x-element behavior
                map(items, item => item.id, item => {
                  return html`<li id="${ item.id }">${ lookup?.[item.id] }</li>`;
                })
              }
              </ul>
              <ul>
              ${
              	// native behavior
                items?.map(item => {
                  return html`<li id="${ item.id }">${ lookup?.[item.id] }</li>`;
                })
              }
              </ul>
              <pre>${JSON.stringify(lookup, null, 2)}</pre>
            </div>
          `;
        };
      }
    }

    customElements.define('demo-179', Demo179);
    </script>
    <script>
    	const demo = document.querySelector('demo-179');
      let flip = 0;
      setInterval(() => {
        if (flip % 2 === 0) {
          demo.lookup = {
            a: 'sizzle',
            b: 'pop',
            c: 'buzz',
          };
        } else {
          demo.lookup = {
            a: 'fizzle',
            b: 'bop',
            c: 'fuzz',
          };
        }
        console.log(demo.lookup);
        ++flip;
      }, 1000);
    </script>
  </body>
</html>

JSFiddle: https://jsfiddle.net/k8de6qcr/1/

Kelketek added a commit to Kelketek/x-element that referenced this issue Oct 8, 2024
Kelketek added a commit to Kelketek/x-element that referenced this issue Oct 8, 2024
Kelketek added a commit to Kelketek/x-element that referenced this issue Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants