Component initialization
tp.Component initialization is fed by three sources:
InitClass() metadata, the constructor CreateParams, and the
data-setup attribute found on markup elements.
This demo shows how those sources become one final params object, when the
HTMLElement is created or resolved, and what code belongs in each lifecycle hook.
1. InitClass()
- Runs from the
tp.Objectconstructor before handle creation. - Initializes pseudo-static, read-only metadata such as
ElementType,ElementSubType,DataValueProperty, andDataBindMode. DataValuePropertyis the current name for the bindable control property.- Derived classes override it to declare the metadata required by their own control type.
2. CreateParams
- Every constructor from
tp.Componentdown receives a singleCreateParamsargument. - It may be a plain object, a
tp.CreateParamsinstance, or null. - When present, its key input is
ElementOrSelector: anHTMLElement, selector, or standard tag name. - A derived constructor may normalize and enrich the same params object before passing it to
super(Params). - When
DeferHandleCreationis true, the constructor does not callCreateHandle(); caller code must call it later.
3. data-setup
- Markup may provide a
data-setupobject with properties supported by the class. CreateHandle()merges it into the same params object; when names collide,data-setupwins.- Only
ApplyCreateParams()should consume the final params.
<input id="ProjectName"
type="text"
data-setup="{ Text: 'TripousJS',
Placeholder: 'project name',
MaxLength: 40,
ToolTip: 'Initialized by data-setup' }" />
4. Prototype defaults
- JavaScript class field initializers run after
super(). - For
tp.Componentdescendants,super()may already have calledApplyCreateParams(). - Use documented prototype defaults for simple non-mutable fields that must be visible as class interface.
- Do not put arrays, objects, collections, handlers, or DOM references on the prototype.
/**
* Custom value applied by create params.
* @type {string}
*/
DemoPrototypeFieldComponent.prototype.CustomText = "";
Initialization flow
ElementOrSelector may be an existing HTMLElement, a selector, a standard
tag name such as div, or null. Existing elements and selector matches set
FromMarkup to true. Tag names and null create a new element, using the
class metadata when needed.
After the final HTMLElement exists, CreateHandle() merges
data-setup over the constructor params, assigns this.CreateParams, and calls
tp.SetObject(Element, this). The element receives the hidden
__tpObject property and the tp-Object CSS class, so Tripous can
find objects associated with page elements.
Hook responsibilities
OnHandleCreated(): handle-only setup such as CSS classes, simple attributes, and handle listeners. Fields are not initialized yet.InitializeFields(): mutable instance fields, flags, arrays, collections, defaults, and bound handler functions.OnFieldsInitialized(): inner DOM, child controls, helper objects, and listeners that must exist before params are applied.ApplyCreateParams(Params): explicitly applies constructor params anddata-setupvalues to object properties.
Avoid
- Do not put metadata such as
ElementType,ElementSubType,DataValueProperty, orDataBindModeinInitializeFields(). - Do not create inner controls in
OnHandleCreated()when they need initialized fields. - Do not read or apply params in
InitializeFields()orOnFieldsInitialized(). - Do not generic-assign all params; each class should explicitly support its own params in
ApplyCreateParams().
Markup handle
The component wraps this existing element. Its data-setup values are
merged into the same create params object before params are applied.
Created handle
This component is not created from markup. Its InitClass() metadata
gives the base creation code enough information to create an
<input type="text">.
Expected order
1. constructor before super() 2. InitClass() 3. CreateHandle() before super 4. base CreateHandle() resolves or creates the handle 5. merge data-setup into the same params object 6. tp.SetObject(Element, this) 7. OnHandleCreated() 8. InitializeFields() 9. OnFieldsInitialized() 10. ApplyCreateParams(Params) 11. CreateHandle() after super 12. constructor after super()