最近在使用extjs4.2 发现要使用Ext.util.Observable这个类和以前不同
extjs4.2的用法
使用extend时
Ext.define("children",{ extend: 'Ext.util.Observable' constructor:function(config); this.callParent(config); } });
使用mixins时
Ext.define("children",{ mixins: { observable: 'Ext.util.Observable' }, constructor:function(config){ this.mixins.observable.constructor.call(this, config); } });
fireEvent 触发事件函数
ext先用addEvents添加事件 再用on方法监听事件 在用fireEvents加载监听
Ext.define("children",{ mixins: { observable: 'Ext.util.Observable' }, constructor:function(config){ this.mixins.observable.constructor.call(this, config); console.log(this.constructor); this.addEvents({"hungry":true}); this.on("hungry",function(milk){ alert(milk); }); this.setMilk=function(milk){ this.fireEvent("hungry",milk); }; } }); var children = Ext.create("children",{}); children.setMilk("sadsads");
relayEvents可以传递事件
Ext.define("father",{ extend:"Ext.util.Observable", constructor:function(config){ this.listeners = config.listeners; this.superclass.constructor.call(this,config); } }); var father = Ext.create("father",{}); father.relayEvents(children,["hungry"]); father.on("hungry",function(){ alert("father"); })
capture可以拦截事件
Ext.util.Observable.capture(children,function(eventName){ if(eventName == "hungry") { alert("我饿了"); } })
addManagedListener可以添加可管理的事件
Ext.create("Ext.toolbar.Toolbar",{ renderTo:document.body, width:500, items:[ {xtype:"button",id:"create",text:"button"}, {xtype:"button",id:"delete",text:"delete"}, {xtype:"button",id:"destroy",text:"destroy",handler:function(){ var c = Ext.getCmp("delete"); if(c){ c.destroy(); } }} ] }); var deletea = Ext.getCmp("delete"); deletea.addManagedListener(Ext.getCmp("create"),"click",function(){ alert("添加操作"); });