我正在扩展mxgraph删除控制示例,以将" delete"控件添加到我的图表中动态生成的节点。该示例的源代码可在此处提供
问题在代码的这一部分中 -
// Overridden to add an additional control to the state at creation time
mxCellRendererCreateControl = mxCellRenderer.prototype.createControl;
mxCellRenderer.prototype.createControl = function(state)
{
mxCellRendererCreateControl.apply(this, arguments);
var graph = state.view.graph;
if (graph.getModel().isVertex(state.cell))
{
if (state.deleteControl == null)
mxcellRendererCreateControl.Apply 内部的createControl的被重新调用似乎可以按预期使用(在创建其他控件之前调用原始函数),并使用load上的图形的初始状态来使用。但是,一旦我动态地将节点添加到图形,并且回调将由MXGraph的validate/redraw调用,则控件进入无限循环,其中"应用"函数基本上会保持自身呼叫(即,回调)。
>我有点不知所措,因为当我调试时,上下文(这个)看起来不错,但是我无法弄清楚为什么它没有调用原型方法,而只是继续在循环中调用覆盖功能。我究竟做错了什么?
看来您的原始功能不正确,请尝试以下操作:
Function.prototype.clone = function() {
var that = this;
return function theClone() {
return that.apply(this, arguments);
};
};
添加主代码中某个地方的新方法,因此它将在整个应用程序中可用,现在您可以将代码更改为:
// Overridden to add an additional control to the state at creation time
let mxCellRendererCreateControl = mxCellRenderer.prototype.createControl.clone();
mxCellRenderer.prototype.createControl = function(state) {
mxCellRendererCreateControl(state);
var graph = state.view.graph;
if (graph.getModel().isVertex(state.cell)) {
if (state.deleteControl == null) {
// ...
}
}
// ...
};
如果我正确理解您的问题,如果没有,请将旧功能调用回到apply
。否则,让我知道Function
原型更改后是否发生了不同的事情。
看来,您的覆盖代码被多次调用(在您的压制代码应该足以对此进行测试之前添加简单的console.log
)
尝试确保仅调用函数的代码一次,或验证原型函数是原始函数还是您的。
这是您如何检查功能是否是您的
的示例if (!mxCellRenderer.prototype.createControl.isOverridenByMe) {
let mxCellRendererCreateControl = mxCellRenderer.prototype.createControl;
mxCellRenderer.prototype.createControl = function(state) { /* ... */ };
mxCellRenderer.prototype.createControl.isOverridenByMe = true;
}
还有其他方法,例如使用全局变量检查您是否已超过了方法。
如果这无法解决您的问题,请发布有关其余代码的更多信息(该代码如何加载/调用将很有帮助)