Issues with Property Data Source in Pluggable Widget

0
Hey community,   I’ve been working on a Pluggable Widget and ran into an issue when using a data source property to feed into the component. When I try to access the data, I get an object reference instead of the actual Mendix data.   I tried doing object.forEach((obj) => obj.get("Title")), for example; but only got the error obj.get is not a function.   After some debugging, I found that I could extract the actual data using:   function getMxObject(obj: any) { const mxSymbol = Object.getOwnPropertySymbols(obj).find(sym => sym.toString() === "Symbol(mxObject)"); return mxSymbol ? obj[mxSymbol] : null; }   This works, but it feels more like a workaround rather than a proper solution. Does anyone know if there’s a better or more official way to access Mendix data from a property data source?
asked
3 answers
0

I solved it using mx.data.get(), but I was under the impression that it was deprecated. 

I am also using a datasource connection, meaning it will fetch all objects, which I then need to fetch again using:

// @ts-ignore
function getMxObject(guid: string, callback: (obj: mendix.lib.MxObject) => void): void {
    // @ts-ignore
    mx.data.get({
        guid,
        callback,
        error: (err: string) => {
            console.error("Failed to retrieve object:", err);
        }
    });
}

So I need to fetch the objects several times, which will eat more bandwidth. 

I am thinking the best way would be to pass all of the objects into the widget and be able to use them in there without having to do a separate get request.

 

The reason I have the @ts-ignores is because I can't for the life of me figure out where to get the types for the Client API. Does anybody have an idea?

The interfaces that doesn't exist are the mendix.lib.MxObject and the mx.data.get().

 

Thanks

answered
0

Hi Pontus,

 

The intended mechanism to get data from Mendix Objects is using an Attribute Property linked to a datasource. The attribute property will provide a `get()` method that accepts the object reference and returns the value as an EditableValue:

 

function ExampleWidget(props) {
  return(
    <ul>
      {props.datasourceProperty.items.map(i => <li>{props.attributeProperty.get(i).displayValue}</li>)}
    </ul>
  );
}

 

This has the benefit that your widget does not become tightly coupled to a specific Entity type. The user of your widget can then specify what attribute of a given entity is the "Title" attribute.

 

I hope this helps,

Arjo

answered
0

Pontus, when you say a helper object, do you mean a non-persistable entity? 

Also, can you share the version of StudioPro and the pluggable widget tools you are using?  

 

answered