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.Object constructor before handle creation.
  • Initializes pseudo-static, read-only metadata such as ElementType, ElementSubType, DataValueProperty, and DataBindMode.
  • DataValueProperty is 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.Component down receives a single CreateParams argument.
  • It may be a plain object, a tp.CreateParams instance, or null.
  • When present, its key input is ElementOrSelector: an HTMLElement, selector, or standard tag name.
  • A derived constructor may normalize and enrich the same params object before passing it to super(Params).
  • When DeferHandleCreation is true, the constructor does not call CreateHandle(); caller code must call it later.

3. data-setup

  • Markup may provide a data-setup object with properties supported by the class.
  • CreateHandle() merges it into the same params object; when names collide, data-setup wins.
  • 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.Component descendants, super() may already have called ApplyCreateParams().
  • 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 and data-setup values to object properties.

Avoid

  • Do not put metadata such as ElementType, ElementSubType, DataValueProperty, or DataBindMode in InitializeFields().
  • Do not create inner controls in OnHandleCreated() when they need initialized fields.
  • Do not read or apply params in InitializeFields() or OnFieldsInitialized().
  • Do not generic-assign all params; each class should explicitly support its own params in ApplyCreateParams().

Markup handle

Initial markup text

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()

Observed lifecycle