2012-11-30 39 views
1

我使用Infragistics的舊版本UltraWebGrid,需要替換一些內置的JavaScript。編譯後的js看起來就像它將一堆函數添加到一個對象類型中作爲一個api類。格式如:重寫JavaScript內置到第三方控件(infragistics UltraWebGrid)

var igtbl_ptsBand = ["functionname1",function(){...},"functionname2",function(){...},... 

等等。我將如何覆蓋這個?

基本上控制是添加HTML到頁面的方式與新的瀏覽器不兼容和JavaScript代碼,這隻需要一點點調整。我找到了代碼...我只需要改變它。

的代碼可以發現here

我添加了一個答案轉儲代碼示例和諸如此類的東西。我不會選擇這個答案

Similar SO question

+1

我已經與這些組件一起工作過,不知怎的,我並不驚訝他們存儲他們的功能完全回籠的方式。 – jbabey

回答

1

你提到的陣列似乎是各種各樣的功能表:

var igtbl_ptsBand = ["func1", function() { }, "func2", function() { } ] 

我會建議使用鏈接,而不是僅僅覆蓋。通過鏈接,您可以注入自己的代碼,但仍然會調用原始函數。假設你想替換「func2」和鏈。你可以做這樣的事情:

var origFunc, findex, ix; 
if (igtbl_ptsBand.indexOf) { 
    // indexOf is supported, use it 
    findex = igtbl_ptsBand.indexOf("func2") + 1; 
} else { 
    // Crippled browser such as IE, no indexOf, use loop 
    findex = -1; 
    for (ix = 0; ix < igtbl_ptsBand.length; ix += 2) { 
     if (igtbl_ptsBand[ix] === "func2") { 
      findex = ix + 1; 
      break; 
     } 
    } 
} 
if (findex >= 0) { 
    // Found it, chain 
    origFunc = igtbl_ptsBand[findex]; 
    igtbl_ptsBand[findex] = function() { 
     // Your new pre-code here 
     // Call original func (chain) 
     origFunc(); 
     // Your new post-code here 
    }; 
} 

origFunc可以有爭論,當然,你可能想使用的JavaScript調用()函數來設置「這個指針」具體的事情,如:

origFunc.call(customThis, arg1, arg2...); 

如果參數在數組中,則可以使用apply()而不是call()。

+0

我需要把這個JavaScript的這個工作?它運行,但舊的JavaScript仍在使用/ – jumpdart

+0

@jumpdart,我不知道你的意思。 –

+0

我添加了一個答案來解釋我正在採取的行動。 – jumpdart

1

我不建議這樣做。你應該總是努力工作第三方庫,而不是它。話雖這麼說,這應該工作:

igtbl_ptsBand[igtbl_ptsBand.indexOf("functionYouWantToOverwrite") + 1] = function() { 
    // your new stuff... 
}; 
+0

我必須解決這些問題,並且有太多開發人員需要許可證才能升級到實際上與瀏覽器兼容的新版本控件,因此這是我唯一的選擇。謝謝BTW ...現在檢查這個 – jumpdart

+0

要小心舊版本的IE(8或更少)不支持indexOf。如果indexOf未定義,您可能需要使用循環。 –

+0

我需要把這個JavaScript的這個工作?它運行,但舊的JavaScript仍在使用 – jumpdart

0

好的,這是我正在做的。我會隨着我的進展更新這個。

這解決了我的問題。事實證明,我必須將函數陣列應用於父「行」的所有子對象。爲此,我在「修復行」功能中添加了代碼。我分裂它,因爲我正在運行其他瀏覽器JS錯誤,我正在修復這個JS文件。

這裏是我加入到我的.NET頁,像這樣的js文件...

</form> 
    <script type="text/javascript" src="../../scripts/BrowserCompat.js"></script> 
</body> 
</html> 

Brows_FixUltraWebGrid(); 

function Brows_FixUltraWebGrid() { 
    FixRows(); 
} 

function FixRows() { 

    FixGridRows_render(); 
    for (var i = 0; i < igtbl_ptsRows.length; i += 2) 
     igtbl_Rows.prototype[igtbl_ptsRows[i]] = igtbl_ptsRows[i + 1]; 

} 


function FixGridRows_render() { 

    var origFunc, findex, ix; 
    if (igtbl_ptsRows.indexOf) { 
     // indexOf is supported, use it 
     findex = igtbl_ptsRows.indexOf("render") + 1; 
    } else { 
     // Crippled browser such as IE, no indexOf, use loop 
     findex = -1; 
     for (ix = 0; ix < igtbl_ptsRows.length; ix += 2) { 
      if (igtbl_ptsRows[ix] === "render") { 
       findex = ix + 1; 
       break; 
      } 
     } 
    } 
    if (findex >= 0) { 
     // Found it, chain 
     origFunc = igtbl_ptsRows[findex]; 
     igtbl_ptsRows[findex] = function() { 
      // Your new pre-code here 
      // Call original func (chain) 
      //origFunc(); 
      // Your new post-code here 
      var strTransform = this.applyXslToNode(this.Node); 
      if (strTransform) { 
       var anId = (this.AddNewRow ? this.AddNewRow.Id : null); 

       //new logic to include tbody if it is not there 
       var tadd1 = ''; 
       var tadd2 = ''; 
       if (!(/\<tbody\>/.test(strTransform))) { 
        tadd1 = '<tbody>'; 
        tadd2 = '</tbody>'; 
       } 
       this.Grid._innerObj.innerHTML = 
        "<table style=\"table-layout:fixed;\">" + tadd1 + strTransform + tadd2 + "</table>"; 

       //old line 
       //this.Grid._innerObj.innerHTML = "<table style=\"table-layout:fixed;\">" + strTransform + "</table>"; 

       var tbl = this.Element.parentNode; 
       igtbl_replaceChild(tbl, this.Grid._innerObj.firstChild.firstChild, this.Element); 
       igtbl_fixDOEXml(); 
       var _b = this.Band; 
       var headerDiv = igtbl_getElementById(this.Grid.Id + "_hdiv"); 
       var footerDiv = igtbl_getElementById(this.Grid.Id + "_fdiv"); 
       if (this.AddNewRow) { 
        if (_b.Index > 0 || _b.AddNewRowView == 1 && !headerDiv || _b.AddNewRowView == 2 && !footerDiv) { 
         var anr = this.AddNewRow.Element; 
         anr.parentNode.removeChild(anr); 
         if (_b.AddNewRowView == 1 && tbl.tBodies[0].rows.length > 0) 
          tbl.tBodies[0].insertBefore(anr, tbl.tBodies[0].rows[0]); 
         else 
          tbl.tBodies[0].appendChild(anr); 
        } 
        this.AddNewRow.Element = igtbl_getElementById(anId); 
        this.AddNewRow.Element.Object = this.AddNewRow; 
       } 
       this.Element = tbl.tBodies[0]; 
       this.Element.Object = this; 
       this._setupFilterRow(); 
       for (var i = 0; i < this.Band.Columns.length; i++) { 
        var column = this.Band.Columns[i]; 
        if (column.Selected && column.hasCells()) { 
         var col = this.getColumn(i); 
         if (col) 
          igtbl_selColRI(this.Grid.Id, col, this.Band.Index, i); 
        } 
       } 
       if (this.ParentRow) { 
        this.ParentRow.ChildRowsCount = this.length; 
        this.ParentRow.VisChildRowsCount = this.length; 
       } 
      } 
      console.log('overridden row render function executed'); 
     }; 
    } 
} 
+0

它取代了函數指針,但您真的需要知道函數指針是否正在重置。你用什麼來調試?我建議console.log超過警報。你應該在替換函數內部放置一個函數,看它是否應該被調用。 –