Add JQHTML-EVENT-01 rule, document custom component events, update jqhtml
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -865,7 +865,7 @@ LIFECYCLE EVENT CALLBACKS
|
||||
(useful for re-renders)
|
||||
- Multiple callbacks: Can register multiple for same event
|
||||
- Chaining: Returns this so you can chain .on() calls
|
||||
- Only lifecycle events allowed: Other event names log error
|
||||
- Custom events also supported (see CUSTOM COMPONENT EVENTS)
|
||||
|
||||
Example - Wait for component initialization:
|
||||
// Initialize nested components after parent ready
|
||||
@@ -881,6 +881,72 @@ LIFECYCLE EVENT CALLBACKS
|
||||
|
||||
Available in JQHTML v2.2.81+
|
||||
|
||||
CUSTOM COMPONENT EVENTS
|
||||
Components can fire and listen to custom events using the jqhtml event
|
||||
bus. Unlike jQuery's .trigger()/.on(), the jqhtml event bus guarantees
|
||||
callback execution even if the event fired before the handler was
|
||||
registered. This is critical for component lifecycle coordination.
|
||||
|
||||
Firing Events:
|
||||
// Fire event from within a component
|
||||
this.trigger('my_event');
|
||||
this.trigger('my_event', { key: 'value' });
|
||||
|
||||
Listening to Events:
|
||||
|
||||
From parent component (using $sid):
|
||||
// In parent's on_ready()
|
||||
this.sid('child_component').on('my_event', (component, data) => {
|
||||
console.log('Event from:', component);
|
||||
console.log('Event data:', data);
|
||||
});
|
||||
|
||||
From external code:
|
||||
$('#element').component().on('my_event', (component, data) => {
|
||||
// Handle event
|
||||
});
|
||||
|
||||
Callback Signature:
|
||||
.on('event_name', (component, data) => { ... })
|
||||
|
||||
- component: The component instance that fired the event
|
||||
- data: Optional data passed as second argument to trigger()
|
||||
|
||||
Key Difference from jQuery Events:
|
||||
jQuery events are lost if fired before handler registration:
|
||||
|
||||
// jQuery - BROKEN: Event fires before handler exists
|
||||
child.$.trigger('initialized'); // Lost!
|
||||
parent.$sid('child').on('initialized', handler); // Never called
|
||||
|
||||
JQHTML events work regardless of timing:
|
||||
|
||||
// JQHTML - WORKS: Event queued, fires when handler registers
|
||||
child.trigger('initialized'); // Queued
|
||||
parent.sid('child').on('initialized', handler); // Called immediately!
|
||||
|
||||
This enables reliable parent-child communication during component
|
||||
initialization, when event timing is unpredictable.
|
||||
|
||||
Example - Child notifies parent of state change:
|
||||
|
||||
// Child component (Tab_Bar.js)
|
||||
set_active_tab(key) {
|
||||
this.state.active_tab = key;
|
||||
this.render();
|
||||
this.trigger('tab:change', { key: key });
|
||||
}
|
||||
|
||||
// Parent component (Settings_Page.js)
|
||||
on_ready() {
|
||||
this.sid('tabs').on('tab:change', (component, data) => {
|
||||
this.show_panel(data.key);
|
||||
});
|
||||
}
|
||||
|
||||
IMPORTANT: Do not use jQuery's .trigger() for custom events in jqhtml
|
||||
components. The code quality rule JQHTML-EVENT-01 enforces this.
|
||||
|
||||
$REDRAWABLE ATTRIBUTE - LIGHTWEIGHT COMPONENTS
|
||||
Convert any HTML element into a re-renderable component using the
|
||||
$redrawable attribute. This parser-level transformation enables
|
||||
|
||||
Reference in New Issue
Block a user