Ace编辑器自动完成功能可上传额外记录

时间:2016-02-11 09:55:09

标签: angularjs ace-editor

我使用angular将ui-ace编辑器添加到我的应用程序中。在应用程序启动期间,我得到json 1次,而不是每次都要求单词。

json-file示例:

[
  {
    "Word": "Do {int} + {int}",
    "Meta": "Implemented"
  },
  {
    "Word": "Line3",
    "Meta": "Not-implemented"
  },
  {
    "Word": "Line2",
    "Meta": "Not-implemented"
  },
  {
    "Word": "Line4",
    "Meta": "Not-implemented"
  },
  {
    "Word": "444",
    "Meta": "Not-implemented"
  },
  {
    "Word": "E1",
    "Meta": "Not-implemented"
  },
  {
    "Word": "E2",
    "Meta": "Not-implemented"
  },
  {
    "Word": "E1Try",
    "Meta": "Not-implemented"
  },
  {
    "Word": "E3",
    "Meta": "Not-implemented"
  },
  {
    "Word": "E4444",
    "Meta": "Not-implemented"
  }
]

问题是自动填充中列出的某些单词多于单词,请查看屏幕截图:http://take.ms/N8BFZ。 这是我如何加载ace-editor,其中ctrl.listStepLines是一个包含来自API的json-response的对象:

$scope.aceLoaded = function(_editor){
    // Editor part
    var _session = _editor.getSession();
    var _renderer = _editor.renderer;

    _editor.$blockScrolling = Infinity;
    _editor.setOptions({
        minLines: 10,
        maxLines: 40,
        wrap: true,
        firstLineNumber: 1,
        enableBasicAutocompletion: true,
        enableSnippets: true,
        enableLiveAutocompletion: true
    })

    var langTools = ace.require("ace/ext/language_tools");
    var rhymeCompleter = {
        getCompletions: function (editor, session, pos, prefix, callback) {
            if (prefix.length === 0) { callback(null, []); return }
            callback(null, ctrl.listStepLines.map(function (ea) {
                return { name: ea.Word, value: ea.Word, meta: ea.Meta }
            }));
        }
    }
    langTools.addCompleter(rhymeCompleter);
};

1 个答案:

答案 0 :(得分:1)

问题是angularjs加载了我的函数很多次,而ace编辑器有14个类似的完成者。我重构了我的代码并为完成加法创建了一个单独的函数,只调用一次。

ctrl.addAutoCompleter();

        function init() {

            ctrl.addAutoCompleter = function () {
                var langTools = ace.require("ace/ext/language_tools");
                var stepLineCompleter = {
                    getCompletions: function (_editor, session, pos, prefix, callback) {
                        if (prefix.length === 0) { callback(null, []); return }
                        callback(null, ctrl.listStepLines.map(function (ea) {
                            return { name: ea.Word, value: ea.Word, meta: ea.Meta }
                        }));
                    }
                }
                langTools.addCompleter(stepLineCompleter);
            }
};