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
feat(base): Add initialization, DOM events wrapper to component API. (google#4915)
* Adds an `initialize` constructor hook that is called after the root
element is attached, but before `getDefaultFoundation` is called.
* Adds a simple way of adding / removing event listeners from a
component's root node, without needing access to the root node itself.
Needed for google#4475
[#126819221]
Copy file name to clipboardExpand all lines: packages/mdl-base/README.md
+38Lines changed: 38 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -156,9 +156,12 @@ export class MyComponent extends MDLComponent {
156
156
157
157
| method | description |
158
158
| --- | --- |
159
+
|`initialize(...args)`| Called after the root element is attached to the component, but _before_ the foundation is instantiated. Any positional arguments passed to the component constructor after the root element, along with the optional foundation 2nd argument, will be provided to this method. This is a good place to do any setup work normally done within a constructor function. |
159
160
|`getDefaultFoundation()`| Returns an instance of a foundation class properly configured for the component. Called when no foundation instance is given within the constructor. Subclasses **must** implement this method. |
160
161
|`initialSyncWithDOM()`| Called within the constructor. Subclasses may override this method if they wish to perform initial synchronization of state with the host DOM element. For example, a slider may want to check if its host element contains a pre-set value, and adjust its internal state accordingly. Note that the same caveats apply to this method as to foundation class lifecycle methods. Defaults to a no-op. |
161
162
|`destroy()`| Subclasses may override this method if they wish to perform any additional cleanup work when a component is destroyed. For example, a component may want to deregister a window resize listener. |
163
+
|`listen(type: string, handler: EventListener)`| Adds an event listener to the component's root node for the given `type`. Note that this is simply a proxy to `this.root_.addEventListener`. |
164
+
|`unlisten(type: string, handler: EventListener)`| Removes an event listener from the component's root node. Note that this is simply a proxy to `this.root_.removeEventListener`. |
162
165
|`emit(type: string, data: Object)`| Dispatches a custom event of type `type` with detail `data` from the component's root node. This is the preferred way of dispatching events within our vanilla components. |
163
166
164
167
#### Static Methods
@@ -173,6 +176,41 @@ In addition to methods inherited, subclasses should implement the following two
173
176
174
177
`MDLComponent` calls its foundation's `init()` function within its _constructor_, and its foundation's `destroy()` function within its own _destroy()_ function. Therefore it's important to remember to _always call super() when overriding destroy()_. Not doing so can lead to leaked resources.
175
178
179
+
#### Initialization and constructor parameters
180
+
181
+
If you need to pass in additional parameters into a component's constructor, you can make use of the
182
+
`initialize` method, as shown above. An example of this is passing in a child component as a
0 commit comments