Pluggable widget: how to access context entity?

4
The Switch/Boolean slider from the appstore uses React and has access to the MxObject. But when I create a pluggable widget (using Yeoman with @mendix/generator-widget), the `props` of the React container does not contain the mxObject (confirmed by issuing a simple `console.log(this.props)` in the component). I noticed that the widget XML does not contain pluginWidget=”true”, whereas the template generated by @mendix/generator-widget does specify pluginWidget=”true” in the XML. If I remove pluginWidget=”true” from my project, I get an error from the typings generator (which wants pluginWidget=”true” to be set). Question out of interest: What kind of widget is the Switch/Boolean slider from the appstore? It uses React, so seems to be a pluggable widget, but it does not use the pluggable widget scaffold, nor does it identify itself as such in the XML. But the main question is still: how can I pass the context entity to the widget?
asked
3 answers
1

Dear Martin,

Great to see you are working with the new pluggable widget framework. The switch/Boolean slider is not a pluggable widget. It is a (temporary) hybrid widget type, it uses React but not the new API, it will be replaced soon by pluggable widget.

When the flag pluginWidget=”true” is set it will become a plugin widget. Plugin widget are declarative and are not responsible any longer for data retrieval, subscriptions or action execution.

So, when you use an attribute, you will receive the new props whenever they change. Just re-render with the value.

See also tutorial https://docs.mendix.com/howto/extensibility/pluggable-widgets

Reference docs: https://docs.mendix.com/apidocs-mxsdk/apidocs/pluggable-widgets

Cheers, Andries

answered
1

Hi Martin, thanks for your question. We don't support entities in Pluggable Widgets, because the properties are resolved out -of-the-box for now. You can check the Pluggable Widgets API to clarify your questions and learn a bit more about Pluggable Widgets.

Check it here https://docs.mendix.com/apidocs-mxsdk/apidocs/pluggable-widgets

answered
0

Hello Martin,

This might be a bit out of date since I’ve not worked with widgets since Mx 6 but we used to declare that the widget requires a context entity in the widget XML, then you’d declare _contextObj in your js which you would then populate on your update call.

XML widget definition:

<widget id="MyWidget.widget.MyWidget" needsEntityContext="true" xmlns="http://www.mendix.com/widget/1.0/">

JS Snippet:


// Internal variables.
        _handles: null,
        _contextObj: null,

 update: function (obj, callback) {
            logger.debug(this.id + ".update");
            this._contextObj = obj;
            this._executeCallback(callback, "_update");
}

Hope this helps

answered