Options: Receive Options
Any component, including a widget component, can receive options. A component can easily define
default values by defining a static variable named defaults
. The received options will be
available over this.options
.
What are we learning here?
- A component can be loaded with
load(ComponentType, ComponentOptions)
- The passed options are available over
this.options[...]
- The default values can be defined in
static defaults = {...}
Let's start pass the options myWidgetArg1
to the widget in our application. We will use here a
further load method signature this.load(ComponentType, ComponentOptions)
:
src/demo/apps/widget-basics/index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Start Definition Object vs Shortcut Signature
We will use here a signature of the load method which expects a Component
and its options.
This is just a shortcut signature. The load method normally expects a so called start definition
object. This would look like:
load({
component: ChildComponent,
options: { myWidgetArg1: "Value 1" }
})
Now we define two options with default values in our widget and pass the final values them into our template:
src/demo/apps/widget-basics/content.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Now we add the values in our template:
src/demo/apps/widget-basics/content.ejs.html
1 2 3 4 5 6 7 8 9 10 11 12 |
|
As you can see you can access all options of a widget over this.options
.
Options that will not be passed to the widget get the values from the defaults.