Table of Contents generated with DocToc
Abstract Component
Twist assumes an "abstract component" specification that is implemented by any view layer framework bindings. For example, React Twist implements everything within this specification using React-specific code.
Inheritance
A component inherits from SignalDispatcher and Disposable. As such, all the methods and functionality provided by those classes are also provided by Twist components, including watch, dispose, trigger, etc.
Component Lifecycle
A Twist component uses the following methods from the React component lifecycle, namely:
constructorrendercomponentWillMountcomponentDidMountcomponentWillUnmount
constructor
When working with other bindings, it might be tempting to call super as you would idiomatically. For example, when working in React, you might want to pass props and context to super. This should be avoided.
constructor() {
super();
/* continue your initialization */
}
render
Most view layers will allow arbitrary logic within the render method. You can, of course, use arbitrary logic within Twist components, but if you want these components to be idiomatic and portable, you should follow these guidelines:
- Only have one
returnvalue - The
returnvalue should return JSX - Don't perform any additional logic within the
rendermethod beyond thereturnstatement - A
refattribute should refer to a class field rather than be a function - Use structural components instead of Javascript constructs to iterate and render conditionally
componentWillMount
This method is called just prior to the component being mounted in the DOM.
componentDidMount
This method is called just after the component is mounted in the DOM.
componentWillUnMount
This method is called just prior to the component being removed from the DOM.
Component APIs
A Twist component provides the following members:
- Properties
scopeis a reference to state shared between a tree of components. It's similar to React'scontext, but easier to use and understand. See Scope for more information.childrenis an observable collection containing all the component's child elements.
- Methods
undeclaredAttributes()allows one to access any extra attributes passed in via a consumer that aren't explicitly defined with an@Attributedecorator. Useful for when you need to spread additional attributes to children.renderChildren([...args])allows one to pass arguments to children using theasattribute.renderChildren(name)enables rendering of namespaced children (for example,<dialog:footer />)renderChildren(name, [...args])allows one to pass arguments to a namespaced child
Component Features
A Twist component must provide for the following:
- Reactive rendering whenever observables and attributes change
- Two-way data binding via
bind:attribute namespace classattributes- Allow
classas an attribute (in addition toclassName - Concatenate multiple
classattributes into a single string - Boolean attributes:
<div class-selected={ this.selected } >will only applyselectedto the element'sclassifthis.selectedistrue.
- Allow
styleattributes- Individual styles can be targeted via
style-*; for example:style-background-color: 'red'. - Styles aren't required to be objects; they can be strings.
- Individual styles can be targeted via
Events
View-layer frameworks take different approaches to event handling. Some will try to hide browser differences between events and others will only pass the actual browser-generated event. Twist components will always pass the underlying browser event. If you need to abstract away browser differences, there are other libraries that can be used to do so.