Builder API
Creating a hierarchy of elements is often awkward and cumbersome when working with GWT Elemental. Even simple structures like
lead to a vast amount of Document.createElement()
and chained Node.appendChild()
calls. With Elemento, creating the above structure is as easy as
The class Elements
provides convenient methods to create the most common elements. It uses a fluent API to create and append elements on the fly. Take a look at the API documentation for more details.
References
When creating large hierarchies of elements, you often need to assign an element somewhere in the tree. Use an inline assignment together with element()
to create and assign the element in one go:
Classes and Interfaces
The builders in Elemento are of one of the following classes:
HTMLContainerBuilder<E extends HTMLElement>
HTMLElementBuilder<E extends HTMLElement>
HTMLInputElementBuilder<E extends HTMLInputElement>
HTMLTextAreaElementBuilder<E extends HTMLInputElement>
For MathML and SVG, the builders are
MathMLElementBuilder<E extends MathMLElement>
andSVGElementBuilder<E extends SVGElement>
All builders encapsulate a given element and implement TypedBuilder<T, B extends TypedBuilder<T, B>>
to make builders work with inheritance. Apart from that, all builders are kept very simple and don't define their own methods. For example, this is the definition of the HTMLElementBuilder
:
The builders get their features solely by implementing specific interfaces. These interfaces contain default methods to manipulate the encapsulated element in a specific way.
The interface names follow the pattern [HTML|MathML|SVG]Element<Scope>Methods
. Some examples are
If you only use Elemento to create a DOM tree quickly and easily, you will most likely hardly come into touch with the method interfaces. However, if you want to create your own builders, these interfaces come in very handy. You can have very fine-grained control over which methods your builder should provide. And since these methods are defined as default methods in interfaces, you are free to mix and match the interfaces across different builders without worrying about multiple inheritance.
The components in PatternFly Java are a good example of this. They build upon Elemento's builders and implement specific method interfaces to define their features.
Last updated