Does the ace core classes keep track of all of the editor instances on a page?

落爺英雄遲暮 提交于 2019-12-12 04:34:12

问题


I'm planning on having multiple ace editor instances on a page and I'd like to know if the core libraries are keeping track of them so I can easily get a reference to them later.

If not, would keeping the editor instances in a dictionary or object be a good way to do it? Could I create an object on the ace class and should they be by reference or 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

And is there an ace destroy method that should be used to remove references to these instances?

Nevermind on part two of this question. I just found the destroy methods:

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

Update:

I just thought of something else. If we can keep track of the id's or references we can prevent same id collisions. It can also help track how many editors are on a page or if multiple are being created by accident.

I just looked at the ace source and don't see anything keeping track of the editors as they are created. Should I try to whip something up or let someone else tackle it?


Update 2:

I'm thinking to add an editors property and set it by id. I've added an answer with a suggestion.


回答1:


Answering my own question, no, it does not. But I suggest the following Pseudo code:

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);

Maybe the editor can be added in the edit call. What do you think?



来源:https://stackoverflow.com/questions/41336542/does-the-ace-core-classes-keep-track-of-all-of-the-editor-instances-on-a-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!