2017-09-26 87 views
2

我正在扩展mxgraph删除控件example以添加删除控件到我的图中动态生成的节点。该示例的源代码可here应用mxgraph无限循环

的问题是在这部分代码 -

  // 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内似乎意在工作(调用在创建附加控件之前的原始功能)与负载上图形的初始状态。但是,一旦我动态地将节点添加到图中,并且回调由mxgraph的validate/redraw调用,控件进入无限循环,其中'apply'函数基本上保持调用自己(即回调)。

我有点无知,因为当我调试时,上下文(this)看起来不错,但我找不到原因而不是调用原型方法,它只是在循环中调用重写的函数。我究竟做错了什么?

+0

你能细说什么'mxCellRendererCreateControl'是,为什么其他地区你重写并使用'prototype'? –

+0

是否可以创建一个最小的在线示例? –

+0

@GhassenLouhaichi mxCellRendererCreateControl是mxCellRenderer.js中的createControl方法。你可以在这里看到它的来源 - https://github.com/jgraph/mxgraph/blob/master/javascript/src/js/view/mxCellRenderer.js,第600行。它创建你想要的基本矩形/形状先在形状顶部绘制并添加删除控件之前先显示。至于为什么我重写它,这只是mxgraph中的推荐方式。您可以在此帖子中发布的第一个示例链接中看到这一点。您可以在节点顶部看到删除图标。这些是由压倒性的。 – Jay

回答

1

看起来你是不是克隆原来的功能以正确的方式,请尝试以下方法:

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原型更改后发生了不同的事情。

+0

感谢您的回答。我试过了,但没有解决问题。 – Jay

+0

有什么变化吗?还是同样的事情发生? –

+0

没有什么变化。相同的结果。 – Jay

1

看来你的压倒一切的代码被称为多次(加入一个简单的console.log您压倒一切的代码之前,应该足以测试这个)

尽量保证它覆盖的函数只被调用一次代码,或验证原型功能是原始功能还是您的原型功能。

这里是你如何检查功能是你的还是一个例子不

if (!mxCellRenderer.prototype.createControl.isOverridenByMe) { 
    let mxCellRendererCreateControl = mxCellRenderer.prototype.createControl; 
    mxCellRenderer.prototype.createControl = function(state) { /* ... */ }; 
    mxCellRenderer.prototype.createControl.isOverridenByMe = true; 
} 

还有其他的方法,比如使用全局变量来检查是否已重写方法还是不行。

如果不能解决您的问题,请发表更多关于你的代码(这是怎么代码加载/被叫将有很大的帮助)

+0

感谢您花时间回答这个问题。感谢你的帮助。 – Jay