Closed chharvey closed 6 years ago
Updated with new xjs.HTMLTemplateElement
methods (v4+):
function populate(data, generator = (f,d) => {}) {
let template = this.querySelector('template')
if (template===null) {
throw new ReferenceError('This list does not have a <template> descendant.')
}
if (template.content.children.length !== 1 || !template.content.children[0].matches('li')) {
throw new ReferenceError ('The <template> must contain exactly 1 <li> element.')
}
this.append(...data.map((datum) =>
new xjs.HTMLTemplateElement(template)
.setRenderer(generator)
.render(datum)
))
return this
}
usage remains same as above
Add this function as instance methods of xjs.HTMLOListElement
and xjs.HTMLUListElement
minor optimizations:
// `this` is an xjs.HTMLElement
let component = new xjs.HTMLTemplateElement(template).setRenderer(generator)
return this.append(
new xjs.DocumentFragment(jsdom.JSDOM.fragment(''))
.append(...data.map((datum) => component.render(datum)))
)
<li>
items to a new document fragment first, and then append that document fragment to the list; saves a lot of document rerendering
Definition
Usage