UEditor:
UEditor - 首页 http://ueditor.baidu.com/website/
我们在对话框上放了几个 UEditor,发现弹出对话框时可能UEditor没有初始化不能将数据设置上去,于是加了一个 setTimeout 反复重试,后来发现关闭对话框后再打开还是不能正确的设置上去,遂有如下代码:
1 2 3 4 5 6 7 8 9 10 11 ( function setTitle(){ var edt = UE.getEditor( "title" ); if (edt == null || $(edt.body).is( ':visible' ) == false ){ setTimeout(setTitle, 10); return ; } edt.ready( function (){ this .setContent(tr.data( "title" )); this .removeListener( 'ready' , arguments.callee) }); })();之后,可以将该玩意儿封装为一个 MOLECULE:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 < div molecule-def="UEditor"> < script > // MOLECULE_DEF function UEditor(){ // 创建UIEditor...... var me = this; function updateContent(){ var edt = UE.getEditor(me.el); if(edt == null || $(edt.body).is(':visible') == false){ setTimeout(updateContent, 10); return; } edt.ready(function(){ this.setContent(me.value); this.removeListener('ready', arguments.callee) }); } this.setValue = function(v){ this.value = v; updateContent(); } this.getValue = function(){ var edt = UE.getEditor(this.el);< br > return edt ? edt.getContent() : this.value; } } // MOLECULE_DEF_END Molecule.create(UEditor) </ script > </ div >该MOLECULE将值存放在 this.value 中,并包装了延迟设置逻辑,http://www.topit.me/album/3649243可以确保总能将值设置成功。
molecule 的两个函数命名为 getValue setValue,可以衔接 d2js 前端渲染器 molecule 和收集器 m。