how to listen list item change on inspector #2891
-
IntroductionWhen I use Inspector, I encount a failure.
Steps to reproduceNo response Restrictions & ConstraintsNo response Does your question relate to JointJS or JointJS+. Select both if applicable.JointJS+ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi falmko, Unfortunately, the inspector always triggers a specific event name for changes in lists (like As a workaround, you can use the graph (or element) listener (see here for details: https://docs.jointjs.com/api/dia/Element/#change), check if the change is coming from inspector (i.e. For example:
|
Beta Was this translation helpful? Give feedback.
-
@zbynekstara Thanks for your suggestion. I get it!Below are generated by AI for recording this issue.JointJS Community Q&A: Optimizing Port Inspector Event Listening MechanismBackgroundWhile developing a graphical editor using the JointJS library, I based my work on the official Port Drag & Drop demo and UML Class Shape Inspector examples to add property editing functionality for ports. 🔍 Key Issue: Temporary Model and Data Binding
Initial Inefficient Solution
// Add separate listeners for each regular property
const propertyKeys = ['name', 'kind', 'namespace', 'priority', 'max_number', 'full', 'role', 'comment'];
propertyKeys.forEach(key => {
generalInspector.on(`change:${key}`, function(value, inputEl) {
_element.portProp(portId, `properties/${key}`, value);
// Special property handling...
});
});
// Add listeners for each possible position in the args list
for(let i = 0; i < 5; i++){
propertyKeys.push(`args/${i}/type`);
propertyKeys.push(`args/${i}/pass_by`);
// ...more properties
}
// Similar approach for handling return properties This approach was inefficient because:
Community QuestionI posted a question on the JointJS community forum, describing my implementation and asking if there was a more efficient solution. Expert AnswerCommunity member zbynekstara pointed out that I could use the tempModel's change event and the propertyPath information in the opt parameter to simplify the code using a single event listener. Implemented SolutionBased on the expert's suggestion, I refactored my code: tempModel.on('change', function(element, opt) {
console.log("tempModel change", element, opt);
// Ensure there is valid change information
if (!opt || !opt.propertyPath || opt.propertyValue === undefined) return;
// Update port property
_element.portProp(portId, `properties/${opt.propertyPath}`, opt.propertyValue);
// Handle special properties
if (opt.propertyPath === 'name') {
_element.portProp(portId, 'attrs/portLabel/text', opt.propertyValue);
} else if (opt.propertyPath === 'kind') {
const portColors = {
'SYNC_INPUT': '#ff9580',
'GUARDED_INPUT': '#ffc880',
'ASYNC_INPUT': '#ffff80',
'OUTPUT': '#80ff95'
};
_element.portProp(portId, 'attrs/portBody/fill', portColors[opt.propertyValue] || '#ff9580');
}
}); JointJS社区问答记录:优化端口Inspector事件监听机制背景情况在使用JointJS库开发图形编辑器时,我基于官方的Port Drag & Drop demo和UML Class Shape Inspector示例,为端口添加了属性编辑功能。 🔍 关键问题:临时模型与数据绑定
初始低效解决方案我最初的解决方案:为每个属性单独添加事件监听器,并为args数组的每个可能位置添加监听器,无论它们是否存在。这种方法效率低下且代码冗余。 // 为每个普通属性添加单独的监听器
const propertyKeys = ['name', 'kind', 'namespace', 'priority', 'max_number', 'full', 'role', 'comment'];
propertyKeys.forEach(key => {
generalInspector.on(`change:${key}`, function(value, inputEl) {
_element.portProp(portId, `properties/${key}`, value);
// 特殊属性处理...
});
});
// 为args列表中的每个可能位置添加监听器
for(let i = 0; i < 5; i++){
propertyKeys.push(`args/${i}/type`);
propertyKeys.push(`args/${i}/pass_by`);
// ...更多属性
}
// 类似的方式处理return属性 这种方法效率低下,原因如下:
社区提问我在JointJS社区论坛上提出了问题,描述了我的实现方式,并询问是否有更高效的解决方案。 专家解答社区成员zbynekstara指出可以利用tempModel的change事件和opt参数中的propertyPath信息来简化代码,使用单一的事件监听器。 实施解决方案根据专家建议,我重构了代码: tempModel.on('change', function(element, opt) {
console.log("tempModel change", element, opt);
// 确保有有效的变更信息
if (!opt || !opt.propertyPath || opt.propertyValue === undefined) return;
// 更新端口属性
_element.portProp(portId, `properties/${opt.propertyPath}`, opt.propertyValue);
// 处理特殊属性
if (opt.propertyPath === 'name') {
_element.portProp(portId, 'attrs/portLabel/text', opt.propertyValue);
} else if (opt.propertyPath === 'kind') {
const portColors = {
'SYNC_INPUT': '#ff9580',
'GUARDED_INPUT': '#ffc880',
'ASYNC_INPUT': '#ffff80',
'OUTPUT': '#80ff95'
};
_element.portProp(portId, 'attrs/portBody/fill', portColors[opt.propertyValue] || '#ff9580');
}
}); |
Beta Was this translation helpful? Give feedback.
Hi falmko,
Unfortunately, the inspector always triggers a specific event name for changes in lists (like
change:args/0/type
) and not a generic one like what works in thewhen
definition.As a workaround, you can use the graph (or element) listener (see here for details: https://docs.jointjs.com/api/dia/Element/#change), check if the change is coming from inspector (i.e.
opt.inspector
is not undefined), and see what the original property path was viaopt.propertyPath
oropt.propertyPathArray
(see here: https://docs.jointjs.com/learn/features/shapes/model-attributes#relationship-between-prop-and-attr-methods).For example: