最近在使用extjs4.2 發現要使用Ext.util.Observable這個類和之前不一樣函數
extjs4.2的用法this
使用extend時
code
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 觸發事件函數get
ext先用addEvents添加事件 再用on方法監聽事件 在用fireEvents加載監聽it
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能夠傳遞事件io
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能夠攔截事件console
Ext.util.Observable.capture(children,function(eventName){ if(eventName == "hungry") { alert("我餓了"); } })
addManagedListener能夠添加可管理的事件event
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("添加操做"); });