Ext事件监听器:
Ext.onReady(function() { Ext.define('children', { extend : 'Ext.util.Observable',//要实现事件监听必须继承Ext.util.Observable类 setMilk : function(milk) { this.fireEvent("hungry", milk); }, constructor : function(configs) { this.state = 'hungry'; this.addEvents({ 'hungry' : true }); this.drunk = function(milk) { alert("孩子喝了一瓶" + milk); }; this.addListener('hungry', function(milk) { if (this.state == 'hungry') { this.drunk(milk); } else { alert("没饿"); } }); this.callParent(configs); } }); var children = Ext.create("children"); Ext.util.Observable.capture(children, function(eventName) {//事件拦截 eventName事件名 if (eventName == 'hungry') { alert("孩子过敏不能喝") return true; } }); Ext.define("father", { extend : 'Ext.util.Observable', constructor : function(config) { this.superclass.constructor.call(this, config); } }); var father = Ext.create("father"); father.on("hungry", function(milk) { alert("父亲送喝了" + milk + "的孩子去医院"); }); father.relayEvents(children, ["hungry"]);//事件传播,将孩子类的hungrg事件传入父亲 children.setMilk('三鹿牛奶'); });
ajax:
Ext.onReady(function() { Ext.get('submit').on('click', function(e, element) { Ext.Ajax.request({ url : 'user.jsp', timeout : 3000, method : 'POST', params : { id : '1'//参数id=1 }, form : 'myform',//提交表单id为myform success : function(response, options) { var obj = Ext .decode(response.responseText); Ext.get(element).set({value:obj.name},false);//改变element的value值 } }); }); });
使用ElementLoader加载数据:
Ext.onReady(function(){ Ext.get('time').getLoader().startAutoRefresh(1000,{//得到Element-->得到ElementLoader-->调用自动刷新方法 url:'time.jsp', params:{id:'2'}, renderer:function(loader,response,request){//渲染时调用(相当于success) var obj=Ext.decode(response.responseText); Ext.get('time').set({value:obj.time},false); } }); });