บล็อกกระบวนการจะบล็อกโมเดลข้อมูลสำรองอ้างอิงทั้งหมดที่กำหนดลายเซ็นของ กระบวนการ (ชื่อ พารามิเตอร์ และการคืนค่า) โมเดลข้อมูลที่ปลั๊กอิน @blockly/block-shareable-procedures มีให้สร้างขึ้นเพื่อจำลองลักษณะการทำงานของบล็อกขั้นตอนที่สร้างไว้ล่วงหน้าเดิมของ Blockly
ซึ่งรวมถึงลักษณะการทำงานบางอย่างที่คุณอาจไม่ต้องการสำหรับบล็อกขั้นตอนที่กำหนดเอง ได้แก่
- ไม่รองรับประเภทการคืนสินค้า
- พารามิเตอร์ทั้งหมดเชื่อมโยงกับตัวแปรส่วนกลาง
หากต้องการลักษณะการทำงานที่แตกต่างออกไป คุณสามารถสร้างโมเดลข้อมูลกระบวนการที่กำหนดเองได้
การใช้งานโมเดลขั้นตอน
โมเดลข้อมูลขั้นตอนต้องใช้
อินเทอร์เฟซ IProcedureModel
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...
}
การใช้งานโมเดลพารามิเตอร์
โมเดลข้อมูลพารามิเตอร์ของคุณต้องใช้
IParameterModel อินเทอร์เฟซ
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...
}
การเปลี่ยนแปลงทริกเกอร์
วิธีการใดก็ตามที่ทําให้เกิดการเปลี่ยนแปลงในโมเดลขั้นตอนควรเรียกใช้ triggerProceduresUpdate จากปลั๊กอิน @blockly/block-shareable-procedures ด้วย ซึ่งจะ
เรียกใช้ doProcedureUpdate ในบล็อกขั้นตอนทั้งหมด ทำให้บล็อกเหล่านั้นแสดงผลอีกครั้ง
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...
}
กิจกรรม
โมเดลกระบวนการในปลั๊กอิน @blockly/block-shareable-procedures จะทริกเกอร์ เหตุการณ์เมื่อมีการแก้ไขกระบวนการด้วย ซึ่งช่วยให้พื้นที่ทำงานหลายแห่งซิงค์กันได้ และแชร์โมเดลขั้นตอนระหว่างพื้นที่ทำงานเหล่านั้นได้ นอกจากนี้ คุณยังเลือก ที่จะทริกเกอร์เหตุการณ์ได้หากต้องการ
การแยกซีเรียลไลซ์
นอกจากนี้ คลาสแต่ละคลาสยังต้องมีstatic loadStateเพื่อรองรับการ
ยกเลิกการซีเรียลด้วย
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...
}