0

我正在使用 TinyMCE,并尝试通过 API 动态加载模板。但是,即使使用基本返回,也不会加载模板(例如将模板列表设置为变量)。它们确实从外部 JSON 文件(硬编码)加载。那么我的问题是:如何返回或呈现自定义 TinyMCE 模板?

例如:

这有硬编码的模板,所以它可以工作:

 templates: "/Content/data/templates.json"

但我正在努力实现(在基本层面上):

templates:
 function () {
   var test = 
        { title: 'Test template 1', content: 'Test 1' };

       return tinymce.util.JSON.parse(test); // doesn't work
      //return JSON.stringify(templates); // doesn't work
    },

在原始规模上(代码不完整):

templates: 
    function () {
            $.getJSON('/Template', function (result) {
              var data = {};
              $.each(result.ResponseObject, function (index, value) {
                data.title = value.Name;
                data.description = value.Name;
                data.content = value.Description;
                 // can't figure out how to return variable 
              });

            });
4

1 回答 1

1

模板选项需要一个Array模板或一个URL(作为 a String) - 您正在向它发送一个函数,这样它就不起作用了。

https://www.tinymce.com/docs/plugins/template/#templates

根据文档...

"If this option is a string then it will be requested as a URL that should produce a JSON output in the same format the option accepts."

...so I would update your server side code to return what the editor expects and just pass that URL in the TinyMCE configuration.

于 2017-05-02T15:21:38.767 回答