Skip to content

Injector

Sergey Mashkov edited this page Jul 7, 2015 · 2 revisions

Injector

Injector is a special extension that provides way to inject single elements to specified class.

🔴 Notice injector is only available for JavaScript-target

import kotlinx.html.injector.*

If there is a class with field

class ExampleBean {
    var myDiv: HTMLDivElement by Delegates.notNull()
    var myP: HTMLParagraphElement by Delegates.notNull()
}

we can inject it by class name or by tag name

val bean = ExampleBean()

document.create.inject(bean, listOf(
        InjectByClassName("my-class") to ExampleBean::myDiv,
        InjectByTagName("p") to ExampleBean::myP
)).div {
    div("my-class") {
        p {
            +"test"
        }
    }
}

⚠️ If there are multiple occurrences found then it will be injected multiple times. Generally this means that the last one will survive

🔴 Beware injector may inject anything to any field of any type so it breaks type safety so use with attention