class ViewComposite
A class that encapsulates a dynamic view.
abstract
class ViewComposite<TView extends View = View> extends View
Description
View composites contain a single (attached) view object. ViewComposite instances are themselves also views, and as such can be included in a view hierarchy or used as the view object of an Activity. When rendered, view composites display the contained view component.
The encapsulated view is referenced by the body property, which is usually set automatically, either when the ViewComposite is instantiated or right before rendering. It may be changed or unlinked at any time; the rendered view will be updated accordingly by the ViewComposite itself.
The body property is watched using ManagedObject.autoAttach, which means that setting it to a view instance automatically attaches the object to the ViewComposite. Setting the property to undefined will unlink the existing view, and setting it to a new view will unlink an existing one. Therefore, the ViewComposite instance can only contain a single view object at a time.
Since the view is attached to the ViewComposite object, bindings continue to work, and events can be handled either by the ViewComposite object or by a containing object.
View composites are primarily used in two different ways:
- As a way to control an encapsulated view. Refer to e.g. UIConditionalView and UIListView, which are built-in ViewComposite classes.
- As a way to create reusable view structures. Refer to the static withPreset() method which can be used to create a ViewComposite class using a function and/or a class.
Note the similarities with the Activity class, which also encapsulates a single view object. As a rule, use activities if event handlers or other class methods include business logic. Use view composites if the class is only concerned with the look and feel of a set of UI components, and all content can be preset, bound, or provided by a view model.
Constructor
- constructor()Creates a new instance of this view composite.
Type Members
- type ViewComposite.WithPreset staticType definition for an extended view composite class, result of ViewComposite.withPreset().
Static Members
- ViewComposite.withPreset(defaults, ViewBody?) staticCreates a reusable view composite class.
Instance Members
- bodyThe encapsulated view object.
- createView() protectedCreates the encapsulated view object, to be overridden if needed.
- beforeRender() protectedA method that’s called before the view is rendered, to be overridden if needed.
- findViewContent(type)Searches the view hierarchy for view objects of the provided type.
- requestFocus()Requests input focus on the contained view object.
- delegateViewEvent(event) protectedDelegates events from the current view.
- render(callback?)Renders the current view, if any.
Inherited Members
- applyViewPreset(preset)Applies the provided preset properties to this object.
- emit(event)Emits an event, immediately calling all event handlers.
- emitChange(name?, data?)Emits a change event, an instance of ManagedChangeEvent.
- listen(handler)Adds a handler for all events emitted by this object.
- isUnlinked()Returns true if the object has been unlinked.
- unlink()Unlinks this managed object.
- beforeUnlink() protectedA method that’s called immediately before unlinking an object, can be overridden.
- attach(target, observer?) protectedAttaches the provided managed object to this object.
- autoAttach(propertyName, observer?) protectedObserves a property, so that any object assigned to it is attached immediately.