You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: learn/guides/ExtendingNeoClasses.md
+62Lines changed: 62 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,68 @@ For every reactive config (`myConfig_`), Neo.mjs provides three optional lifecyc
61
61
***Purpose**: Intercepts the value *before* it is returned by the getter. Useful for lazy initialization, computing values on demand, or returning a transformed version of the stored value.
62
62
***Return Value**: Return the `value` that should be returned by the getter.
63
63
64
+
### Overriding Lifecycle Hooks: `super` vs. Full Override
65
+
66
+
When extending a Neo.mjs class, you often need to customize the behavior of inherited lifecycle hooks (like `afterSet*`, `onConstructed`, etc.). You have two primary approaches:
This is the most common and recommended approach. By calling `super.methodName(...)`, you ensure that the parent class's implementation of the hook is executed. You can then add your custom logic either before or after the `super` call.
71
+
72
+
This approach is crucial for maintaining the framework's intended behavior and ensuring that inherited features continue to function correctly.
73
+
74
+
```javascript readonly
75
+
importButtonfrom'../../src/button/Base.mjs';
76
+
77
+
classMyExtendedButtonextendsButton {
78
+
static config = {
79
+
className:'My.Extended.Component',
80
+
// text_ config is inherited from Button.Base
81
+
// We can set a default value here if needed, or rely on button.Base's default
82
+
text:'New Default Text'
83
+
}
84
+
85
+
// Example: Adding logic after the parent's afterSetText
86
+
afterSetText(value, oldValue) {
87
+
// Add your custom pre-processing logic here
88
+
super.afterSetText(value, oldValue);
89
+
console.log(`Custom logic: Button text changed to "${value}"`);
90
+
// Add your custom post-processing logic here
91
+
}
92
+
}
93
+
94
+
exportdefaultNeo.setupClass(MyExtendedButton);
95
+
```
96
+
97
+
#### 2. Completely Overriding Parent Behavior (No `super` Call)
98
+
99
+
In rare cases, you might want to completely replace the parent class's implementation of a hook. This is achieved by simply omitting the `super` call within your overridden method.
100
+
101
+
**Caution**: Use this approach with extreme care. You must fully understand the parent's implementation and ensure that your override does not break essential framework functionality or inherited features. This is generally reserved for advanced scenarios where you need full control over the hook's execution.
102
+
103
+
```javascript readonly
104
+
importButtonfrom'../../src/button/Base.mjs';
105
+
106
+
classMyFullyOverriddenButtonextendsButton {
107
+
static config = {
108
+
className:'My.Fully.Overridden.Component',
109
+
text :'New Default Text'
110
+
}
111
+
112
+
// Example: Completely overriding afterSetText
113
+
afterSetText(value, oldValue) {
114
+
// No super.afterSetText(value, oldValue); call
115
+
console.log(`Fully custom logic: Button text changed to "${value}"`);
116
+
// The parent's afterSetText will NOT be executed
117
+
// This means that in this case you need to take care on your own to map the text value to the vdom.
0 commit comments