I understand how to use imports: {}
and exports: {}
to share a UI components properties, such as:
defaults: {
exports: {
shouldShowMessage: '${$.component}'
}
}
Which returns the component name in the exports.
But when I try to export a Knockout observable it is always undefined:
defaults: {
exports: {
shouldShowMessage: '${$.shouldShowMessage}'
}
}
...
setupKoBindings: function() {
this.shouldShowMessage = ko.observable('Testing');
}
As a workaround I am going to create a storage model as explained here but I would prefer to use the imports and exports.
Best Answer
The values of the exports object have to resolve to a name and a property of a UiComponent instance, separated by a ':', for example
checkout.cart.total:title
.The export target name has to include the UI component "namespace".
In your example, you set the value to a string, which resolves to a property of the UiComponent that is the export source. The export is undefined when you inspect it because that is not a valid export target.
Here is an example that works:
The above will copy the value of the
shouldShowMessage
property into the propertyshowMessage
of a UiComponent with the full namefoo.bar
every time the value changes.Note that this will not automatically make the target property a KO observable, too. That has to be declared explicitly, if value changes should trigger KO to rerender DOM nodes that access that property.
By the way, adding
shouldShowMessage
to thetracks
object will make it a ko-es5 observable automagically. Using a literalko.observable()
works, too.In the example above, the
exportTarget
andexportTargetProperty
are configured in thedefaults
. They also could be specified as part of the UiComponent options in the JSON, which usually makes more sense, since that is where the UiComponent hierarchy including the UiComponent names are defined.Finally, I would like to note that I personally think your solution using a value object to pass the value to the other UI component is better than using exports or imports. In my experience keeping shared state in the DOM or in UiComponents is a recipe for spaghetti OOP in all but the simplest cases.