2016-12-26 38 views
0

我打算在頁面上有多個ace編輯器實例,我想知道核心庫是否正在跟蹤它們,以便稍後可以輕鬆地獲取它們的參考。ACE核心類是否跟蹤頁面上的所有編輯器實例?

如果沒有,將編輯器實例保存在字典或對象中是否是一種很好的方法?我可以在ace類上創建一個對象,並且應該通過引用還是id?

var editor1 = ace.edit("myEditorDivID"); 
var editor2 = ace.edit("myEditorDivID2"); 
var editors = ace.editors; 

console(editor1==editors["myEditorDivID"]); // true 
console.log(editors["myEditorDivID"]); // editor1 

var editorIds = ace.editorIds; 

console.log(editorIds[0]); // myEditorDivID 

是否有一個ace destroy方法應該用於刪除對這些實例的引用?

沒有關於這個問題的第二部分。我剛剛發現的破壞方法:

editor.destroy(); 
editor.container.remove(); 

更新:

我只是想到了別的東西。如果我們可以跟蹤id或引用,我們可以阻止相同的id衝突。它還可以幫助追蹤頁面上有多少編輯者,或者偶然創建多個編輯者。

我剛纔看着ace source,並沒有看到任何東西跟蹤編輯器,因爲它們被創建。我是否應該嘗試鞭打某事或讓其他人解決它?


更新2:

我想添加一個editors財產和ID設置。我已經添加了一個建議的答案。

+0

有沒有辦法讓所有的ace編輯器實例? –

回答

0

回答我自己的問題,不,它不。但我建議使用下面的僞代碼:

ace.addEditorById = function (id, editor) { 
    if (ace.editors[id]!=null) throw Error ("Editor already created"); 
    ace.editors[id] = editor; 
} 

ace.getEditorById = function (id) { 
    return ace.editors[id]; 
} 

ace.removeEditorById = function (id) { 
    var editor = ace.editors[id]; 
    if (editor) { 
     editor.destroy(); 
     editor.container.remove(); 
     delete ace.editors[id]; 
    } 
} 

ace.editors = {}; 

// then when I create an editor I use the following code: 
editor = ace.edit("editor1"); 
ace.addEditorById(editor); 
editor2 = ace.edit("editor2"); 
ace.addEditorById(editor2); 

也許編輯器可以在編輯調用中添加。你怎麼看?

相關問題