查看文档,您可以将启动数据传递给小部件:
editor.execCommand( 'simplebox', {
startupData: {
align: 'left'
}
} );
然而,这些数据是毫无意义的,因为似乎没有办法影响模板输出 - 它已经在小部件的初始化之前生成,而且数据在那时甚至不可用:
editor.widgets.add('uselesswidget', {
init: function() {
// `this.data` is empty..
// `this.dataReady` is false..
// Modifying `this.template` here does nothing..
// this.template = new CKEDITOR.template('<div>new content</div>');
// Just after init setData is called..
this.on('data', function(e) {
// `e.data` has the data but it's too late to affect the tpl..
});
},
template: '<div>there seems to be no good way of creating the widget based on the data..</div>'
});
此外添加 CKEditor 标记会引发“未捕获的 TypeError:无法读取未定义的属性 'align'”异常,因此数据似乎也没有传递给原始模板:
template: '<div>Align: {align}</div>'
CKEDITOR.template.output
如果无法动态传递数据,那么拥有一个可以接受上下文的函数有什么意义?
到目前为止,我发现的唯一可怕的 hacky 解决方案是拦截 a 中的命令beforeCommandExec
并阻止它,然后修改template
并再次手动执行命令。
根据传递的数据生成动态模板的任何想法?谢谢。