`list.reIndex()` Omits `self.update()`, Leaving `visibleItems` and `matchingItems` Empty
Affected File & Line
- File:
src/index.js - Lines: 98–105 (
this.reIndex)
Description
The documented purpose of reIndex() is to re-parse the DOM list when elements are added or modified externally.
In src/index.js:
this.reIndex = function () {
self.items = []
self.visibleItems = []
self.matchingItems = []
self.searched = false
self.filtered = false
self.parse(self.list)
}
reIndex() resets self.items, self.visibleItems, and self.matchingItems, then calls self.parse(self.list).
While self.parse() populates self.items, it does not populate self.visibleItems or self.matchingItems.
Every other method in List.js that mutates items (init.start, add, remove, show, search, filter, sort) ends with a call to self.update(). reIndex() completely omits self.update().
Real-World Impact
Following a call to list.reIndex():
list.visibleItemsis empty ([]).list.matchingItemsis empty ([]).- Items have not had
.show()or.hide()applied. - The
'updated'event is never dispatched. - Connected plugins and pagination components remain completely desynchronized from the actual DOM state.
Steps to Reproduce
- Initialize a
Listwith 1 item:list.items.length === 1,list.visibleItems.length === 1. - Append a new
<li>element tolist.listin the DOM. - Call
list.reIndex(). - Inspect
list.visibleItems.lengthandlist.matchingItems.length.
Minimal Reproduction Code (Jest / Node.js)
const List = require('./src/index');
document.body.innerHTML = '<div id="list"><ul class="list"><li><span class="name">Item 1</span></li></ul></div>';
const list = new List('list', { valueNames: ['name'] });
console.log('Initial visibleItems:', list.visibleItems.length); // 1
// Add element to DOM and re-index
const li = document.createElement('li');
li.innerHTML = '<span class="name">Item 2</span>';
list.list.appendChild(li);
list.reIndex();
console.log('After reIndex items:', list.items.length); // 2
console.log('After reIndex visibleItems:', list.visibleItems.length); // Actual: 0, Expected: 2
console.log('After reIndex matchingItems:', list.matchingItems.length); // Actual: 0, Expected: 2
Expected Behavior
After list.reIndex(), visibleItems and matchingItems contain the 2 parsed items, and the 'updated' event fires.
Actual Behavior
list.items.length is 2, but list.visibleItems and list.matchingItems are [] (length 0).
Root Cause
reIndex() forgets to call self.update().
Proposed Fix
--- a/src/index.js
+++ b/src/index.js
@@ -102,6 +102,7 @@ module.exports = function (id, options, values) {
self.searched = false
self.filtered = false
self.parse(self.list)
+ self.update()
}
this.toJSON = function () {