Create custom procedure data models
Stay organized with collections
Save and categorize content based on your preferences.
Procedure blocks all reference backing data models which define the signature of
the procedure (name, parameters, and return). The data models provided by the
@blockly/block-shareable-procedures plugin are built to
replicate the behavior of Blockly's legacy built-in procedure blocks.
This includes some behavior that you may not want for your custom procedure
blocks, including:
- Return types are not supported
- All parameters are associated with a global variable
If you want different behavior, you can create your own custom procedure data
models.
Procedure model implementation
Your procedure data model needs to implement the
IProcedureModel interface.
class MyProcedureModel {
constructor(workspace, name, id) {
this.workspace = workspace;
this.name = name;
this.id = id;
// Note: construction should not add the model to the procedure map.
},
// Other methods are omitted for brevity...
}
Parameter model implementation
Your parameter data model needs to implement the
IParameterModel interface.
class MyParameterModel {
constructor(workspace, name, id) {
this.workspace = workspace;
this.name = name;
this.id = id;
},
setProcedureModel(model) {
this.model = model;
return this;
}
// Other methods are omitted for brevity...
}
Trigger changes
Any of the methods that trigger changes to the procedure model should also call
triggerProceduresUpdate
from the
@blockly/block-shareable-procedures plugin. This will
call doProcedureUpdate
on any procedure blocks, causing them to rerender.
import {triggerProceduresUpdate} from '@blockly/block-shareable-procedures';
class MyProcedureModel {
setName(name) {
this.name = name;
triggerProcedureUpdate();
return this;
}
// Other methods are omitted for brevity...
}
class MyParameterModel {
setName(name) {
this.name = name;
triggerProcedureUpdate();
return this;
}
// Other methods are omitted for brevity...
}
Events
The procedure models in the
@blockly/block-shareable-procedures plugin also fire
events when the procedures are modified. This allows multiple workspaces to be
kept in sync, and procedure models to be shared across them. You can also choose
to fire events if you want.
Deserialization
Your classes also each need a static loadState
method to support
deserialization.
class MyProcedureModel {
static loadState(state, workspace) {
// Note that the procedure model should not deserialize parameters.
// The deserializer will handle that.
return new MyProcedureModel(workspace, state.name, state.id);
}
// Other methods are omitted for brevity...
}
class MyParameterModel {
static loadState(state, workspace) {
return new MyParameterModel(workspace, state.name, state.id);
}
// Other methods are omitted for brevity...
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-09-18 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["\u003cp\u003eProcedure blocks rely on data models to define their signature (name, parameters) and the provided plugin replicates Blockly's legacy behavior, including associating parameters with global variables and lacking return type support.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can create custom data models for procedures and parameters by implementing the \u003ccode\u003eIProcedureModel\u003c/code\u003e and \u003ccode\u003eIParameterModel\u003c/code\u003e interfaces respectively, offering control over behavior.\u003c/p\u003e\n"],["\u003cp\u003eTo reflect changes in procedure blocks, developers need to call \u003ccode\u003etriggerProceduresUpdate\u003c/code\u003e after modifying procedure or parameter models.\u003c/p\u003e\n"],["\u003cp\u003eThe provided plugin fires events upon procedure modification to enable synchronization across workspaces, a feature that custom models can optionally implement.\u003c/p\u003e\n"],["\u003cp\u003eBoth custom procedure and parameter model classes require a static \u003ccode\u003eloadState\u003c/code\u003e method for deserialization to reconstruct models from saved states.\u003c/p\u003e\n"]]],["Procedure blocks reference data models that define their signature. The `@blockly/block-shareable-procedures` plugin offers models replicating legacy behavior, lacking return types and linking parameters to global variables. Custom models can be created by implementing `IProcedureModel` and `IParameterModel` interfaces. Modifying methods should call `triggerProceduresUpdate` to rerender blocks. The plugin's models trigger events for synchronization, and models must include a `static loadState` method for deserialization.\n"],null,["Procedure blocks all reference backing data models which define the signature of\nthe procedure (name, parameters, and return). The data models provided by the\n[@blockly/block-shareable-procedures](https://www.npmjs.com/package/@blockly/block-shareable-procedures) plugin are built to\nreplicate the behavior of Blockly's legacy built-in procedure blocks.\n\nThis includes some behavior that you may not want for your custom procedure\nblocks, including:\n\n- Return types are not supported\n- All parameters are associated with a global variable\n\nIf you want different behavior, you can create your own custom procedure data\nmodels.\n\nProcedure model implementation\n\nYour procedure data model needs to implement the\n[IProcedureModel](/blockly/reference/js/blockly.procedures_namespace.iproceduremodel_interface) interface. \n\n class MyProcedureModel {\n constructor(workspace, name, id) {\n this.workspace = workspace;\n this.name = name;\n this.id = id;\n\n // Note: construction should not add the model to the procedure map.\n },\n\n // Other methods are omitted for brevity...\n }\n\nParameter model implementation\n\nYour parameter data model needs to implement the\n[IParameterModel](/blockly/reference/js/blockly.procedures_namespace.iproceduremodel_interface) interface. \n\n class MyParameterModel {\n constructor(workspace, name, id) {\n this.workspace = workspace;\n this.name = name;\n this.id = id;\n },\n\n setProcedureModel(model) {\n this.model = model;\n return this;\n }\n\n // Other methods are omitted for brevity...\n }\n\nTrigger changes\n\nAny of the methods that trigger changes to the procedure model should also call\n`triggerProceduresUpdate` from the\n[@blockly/block-shareable-procedures](https://www.npmjs.com/package/@blockly/block-shareable-procedures) plugin. This will\ncall `doProcedureUpdate` on any procedure blocks, causing them to rerender. \n\n import {triggerProceduresUpdate} from '@blockly/block-shareable-procedures';\n\n class MyProcedureModel {\n setName(name) {\n this.name = name;\n triggerProcedureUpdate();\n return this;\n }\n\n // Other methods are omitted for brevity...\n }\n\n class MyParameterModel {\n setName(name) {\n this.name = name;\n triggerProcedureUpdate();\n return this;\n }\n\n // Other methods are omitted for brevity...\n }\n\nEvents\n\nThe procedure models in the\n[@blockly/block-shareable-procedures](https://www.npmjs.com/package/@blockly/block-shareable-procedures) plugin also fire\nevents when the procedures are modified. This allows multiple workspaces to be\nkept in sync, and procedure models to be shared across them. You can also choose\nto fire events if you want.\n\nDeserialization\n\nYour classes also each need a `static loadState` method to support\ndeserialization. \n\n class MyProcedureModel {\n static loadState(state, workspace) {\n // Note that the procedure model should not deserialize parameters.\n // The deserializer will handle that.\n return new MyProcedureModel(workspace, state.name, state.id);\n }\n\n // Other methods are omitted for brevity...\n }\n\n class MyParameterModel {\n static loadState(state, workspace) {\n return new MyParameterModel(workspace, state.name, state.id);\n }\n\n // Other methods are omitted for brevity...\n }"]]