2017-09-26 86 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