nbhi 0.6.13 → 0.6.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -272,6 +272,8 @@ index.html
272
272
 
273
273
  Same logic for `<ol>`, `<ul>` and `<option>`
274
274
 
275
+ When a record has an `id` (string or number), its rendered element is reused for the next record with the same `id`, even when that is a new object (for example after reloading the data). The element stays in the DOM and is updated in place. Records without an `id` are matched by object identity.
276
+
275
277
  ## Lazy loading
276
278
 
277
279
  ```js
@@ -99,11 +99,6 @@ export default selector => {
99
99
 
100
100
  function updateList(records) {
101
101
  const parent = getParent();
102
-
103
- if (!templateChild) {
104
- throw new Error(`Cannot update ${ selector }, cannot find child element`);
105
- }
106
-
107
102
  const newChildren = new Map();
108
103
 
109
104
  records.forEach((record, index) => {
@@ -146,6 +141,10 @@ export default selector => {
146
141
 
147
142
  function getId(record, index) {
148
143
  if (record && typeof record === 'object') {
144
+ const recordId = record.id;
145
+ if (['string', 'number'].includes(typeof recordId) && !newChildren.has(`id:${ recordId }`)) {
146
+ return `id:${ recordId }`;
147
+ }
149
148
  if (!idCache.has(record)) {
150
149
  idCache.set(record, idCount++);
151
150
  }
@@ -158,7 +157,6 @@ export default selector => {
158
157
  if (templateParent) {
159
158
  return templateParent;
160
159
  }
161
-
162
160
  const topLevel = getTopLevel();
163
161
  if (topLevel.localName === 'table') {
164
162
  return process('tbody', 'tr');
@@ -170,6 +168,7 @@ export default selector => {
170
168
  return process('select', 'option');
171
169
  } else if (topLevel.querySelector('[child]')) {
172
170
  const parent = getParent(topLevel.querySelector('[child]')).parentElement;
171
+ templateParent = parent;
173
172
  setTemplateChild('[child]', parent);
174
173
  if (parent.localName === 'slot' && parent.hasAttribute('name')) {
175
174
  templateChild.slot = parent.getAttribute('name');
@@ -183,6 +182,7 @@ export default selector => {
183
182
  function process(parentName, childName) {
184
183
  const parent = getParent(parentName);
185
184
  setTemplateChild(childName, parent);
185
+ templateParent = parent;
186
186
  return parent;
187
187
  }
188
188
 
@@ -203,7 +203,6 @@ export default selector => {
203
203
  console.log({ topLevel, parentName });
204
204
  throw new Error('Cannot find parent');
205
205
  }
206
- templateParent = parent;
207
206
  return parent;
208
207
  }
209
208
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nbhi",
3
- "version": "0.6.13",
3
+ "version": "0.6.16",
4
4
  "description": "No-Build HTML Includes (NBHI) is a simple library that allows you to include html files into other html files and turns them into [web components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components). Lazy initialization of content (when entering the viewport) and a one line call to update/hydrate content in your html pages. No build tools required, no `package.json` needed.",
5
5
  "keywords": [
6
6
  "web-components",