ace核心类是否可以跟踪页面上的所有编辑器实例? [英] Does the ace core classes keep track of all of the editor instances on a page?

查看:128
本文介绍了ace核心类是否可以跟踪页面上的所有编辑器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划在页面上有多个ace编辑器实例,并且我想知道核心库是否在跟踪它们,以便以后可以轻松地引用它们。

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.

如果没有,将编辑器实例保留在字典或对象中是否是一种很好的方法?我可以在ace类上创建一个对象,并且应该通过引用还是id来创建它们?

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

是否应该使用ace destroy方法删除对这些实例的引用?

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






更新:

我只是想到了别的东西。如果我们可以跟踪ID或引用,则可以防止相同的ID冲突。它还可以帮助跟踪页面上有多少编辑器,或者是否偶然创建了多个编辑器。

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.

我只是看着 ace来源,并且在创建编辑器时看不到任何可跟踪它们的信息。我应该试着鞭打还是让别人来解决?

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?

更新2:

我正在考虑添加编辑器属性,并按ID进行设置。我添加了一个建议。

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

推荐答案

回答我自己的问题,不,不是。但我建议使用以下伪代码:

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?

这篇关于ace核心类是否可以跟踪页面上的所有编辑器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆