Custom Model Extension Not Working

Hi, if I have a definition of my metamodel instantiated in my modeler, like:

new Modeler({
  moddleExtensions: {
          my: MY_META_MODEL,
  }
})

and in MY_META_MODEL i have:

{
  name: 'MY_META_MODEL',
  prefix: 'my',
  uri: 'http://my.org/bpmn',
  xml: {
    tagAlias: 'lowerCase',
  },
  associations: [],
  types: [
    {
      name: 'CustomNode',
      superClass: ['Element'],
      properties: [{ name: 'textBody', isBody: true, type: 'String' }],
    },
  ],
}

and then when I create a node and update extension properties like

 const newExtension = moddle.create('my:CustomNode', { ...someProps });
          extensionElements.values = [newExtension];
          modeling.updateProperties(element, {
            extensionElements,
          });

everything works fine. But if I try to create an extension field with something other than my:CustomNode, i.e. my:Test, modeler will throw an exception saying unknown type <my:Test>.

My question: is it possible to somehow define and allow any node be a valid extension element, even if it is not defined in my meta model?

You can use createAny for this purpose (cf. moddle/moddle.js at master · bpmn-io/moddle · GitHub). However, if create isn’t working for you, you should try and fix the issue. Where are you getting moddle from?

Ah I’ll try with createAny… As for the moddle, i’m getting it from the properties’ panel useService hook

If you have created a model extension you should try and get that to work instead of using createAny.

Sorry, multiple edits of me just being more confused, so I’ll post one concise one now:
using createAny, how can i specify that one of the fields should be the node text body instead of a property?
i.e. <thing>body</thing> instead of <thing text="body" /> ?
Tried all of these yet they all bind to a property:

moddle.createAny('thing', '', {
            text: 'text',
            body: 'body',
            textBody: 'textBody',
          })

createAny does not allow you to define a property as the body. As I said, you should try and fix your code so your model extension works. I’ve created a working example. Have a look at your code and try to figure out why it’s not working.

const myModdleExtension = {
  name: "MY_META_MODEL",
  prefix: "my",
  uri: "http://my.org/bpmn",
  xml: {
    tagAlias: "lowerCase"
  },
  associations: [],
  types: [
    {
      name: "CustomNode",
      superClass: ["Element"],
      properties: [{ name: "textBody", isBody: true, type: "String" }]
    }
  ]
};

const modeler = new Modeler({
  ...
  moddleExtensions: {
    my: myModdleExtension
  }
});

...

const canvas = modeler.get("canvas");

const moddle = modeler.get("moddle");

const extensionElements = moddle.create("bpmn:ExtensionElements");

const customNode = moddle.create("my:CustomNode", {
  textBody: "foobar"
});

customNode.$parent = extensionElements;

extensionElements.set("values", [customNode]);

const process = canvas.getRootElement();

const modeling = modeler.get("modeling");

modeling.updateProperties(process, { extensionElements });

CodeSandbox: Custom Meta Model Example - CodeSandbox

Yeah this works already for the specific defined properties in the model, but I didn’t know that createAny can’t have body. Thanks for answering :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.