Manually compiles a page fragment that has been inserted into the DOM by external code.
All registered compilers and macros will be called
with matches in the given element
.
The up:fragment:inserted
event is emitted on the compiled element.
up.hello()
When the page is manipulated using Unpoly functions or HTML selectors,
Unpoly will automatically call up.hello()
on new fragments:
let link = document.querySelector('a[href]')
let { fragment } = await up.follow(link)
// The fragment is already compiled. No need to call up.hello().
You only ever need to use up.hello()
after creating DOM elements without Unpoly's involvement, for example:
document.createElement()
.innerHTML
property on an existing elementDOMParser()
.In this case compilers will not run automatically and some of Unpoly's own HTML selectors will not be active.
We can address this by manually calling up.hello()
on new elements:
let element = document.createElement('div')
element.innerHTML = '<a href="/path" up-follow>click me</a>'
// The element is not compiled yet. We must compile it manually:
up.hello(element)
It is safe to call up.hello()
multiple times with the same elements.
In particular every compiler function is guaranteed to only run once for each matching element.
If a new compiler is registered after initial compilation, that new compiler is run automatically on current elements.
If a compiler function throws an error, up.hello()
will still finish the compilation and not throw an error.
Instead compiler errors will print to the error console
and emit an error
event on window
:
window.addEventListener('error', function(error) {
alert("Got an error " + error.name)
})
up.hello(element)
See errors in user code for details.
Overrides properties from the new fragment's [up-data]
with the given data object.
An object containing information about this compiler pass.
This typically contains { request, response, revalidating }
properties.
It will be passed as a third compiler argument.
The compiled element