本文主要是介绍EXT事件绑定,父级回调子级计算结果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、addListener方法
- var button = Ext.get('btn');
- button.addListener('click',hello1);
- button.addListener('click',hello2);
二、ExtJS支持的自定义事件
-
- var Person = Ext.extend(Ext.util.Observable,{
- constructor : function(name){
- this.name = name;
- this.sayNum = 0;
- this.say = function(){
- if(this.sayNum < 2){
- this.sayNum += 1;
- alert('I am '+name);
- }else{
- this.sayNum = 0;
- this.fireEvent('onSay',this);
- }
- }
- this.addEvents({
- "onSay" : true
- });
- }
- });
- var per = new Person('tom',3);
-
- per.addListener('onSay',function handler(){
- alert('发生了自定义事件');
- });
- <input type='button' value='say' onclick='per.say()'>
自定义事件的另外一个例子:
-
- var Person = Ext.extend(Ext.util.Observable,{
- constructor : function(name){
- this.name = name;
- this.say = function(){
- this.fireEvent('onSay',this.name);
- }
- this.addEvents({
- "onSay" : true
- });
- }
- });
- var per = new Person('tom');
-
-
- per.addListener('onSay',function(name){
- alert("I'am " + name);
- });
拦截器使用示例Observable.capture,与上面例子不同的地方是“var per = new Person('tom');//创建对象”代码之后的内容替换为:
- per.addListener('onSay',handler);
- function handler(){
- alert('发生了自定义事件');
- }
-
- Ext.util.Observable.capture(per,captureFunction);
-
- function captureFunction(eventName){
- if(eventName == 'onSay'){
- alert("拦截事件:“"+eventName+"”。");
- return false;
- }
- return true;
- }
三、addManagedListener受管事件监听器:
- function createFn(){
- alert('新建');
- }
- function openFn(){
- alert('打开');
- }
- function saveFn(){
- alert('保存');
- }
- Ext.create('Ext.toolbar.Toolbar',{
- renderTo: Ext.getBody(),
- bodyPadding: 5,
- width:300,
- items : [
- {text:'新建',id:'createBtn',iconCls:'newIcon'},
- {text:'打开',id:'openBtn',iconCls:'openIcon'},
- {text:'保存',id:'saveBtn',iconCls:'saveIcon'},
- {text:'销毁新建按钮',handler : function(){
-
- Ext.getCmp('createBtn').destroy();
- }}
- ]
- });
- var createBtn = Ext.getCmp('createBtn');
- createBtn.on('click',createFn);
-
-
- createBtn.addManagedListener(Ext.getCmp('openBtn'),'click',openFn);
- createBtn.addManagedListener(Ext.getCmp('saveBtn'),'click',saveFn);
四、relayEvents传播分发事件:
-
- var Employee = Ext.extend(Ext.util.Observable,{
- constructor : function(config){
- this.name = config.name;
- this.eat = function(){
- this.fireEvent('startEat',this);
- this.fireEvent('finishEat',this);
- }
- this.addEvents({
- "startEat" : true,
- "finishEat" : true
- });
- Employee.superclass.constructor.call(this, config)
- }
- });
-
-
- var Waiter = Ext.extend(Ext.util.Observable,{
- constructor : function(config){
- Waiter.superclass.constructor.call(this, config);
- }
- });
-
- var emp = new Employee({name:'tom'});
-
- emp.addListener('startEat',function(){
- alert(this.name + '开始吃饭了。');
- });
- emp.addListener('finishEat',function (){
- alert(this.name + '吃完饭,可以收拾盘子了。');
- });
-
- var waiter = new Waiter();
-
-
-
- waiter.relayEvents(emp, ['finishEat']);
- waiter.on('finishEat',function(){
- alert('服务员开始收拾盘子。');
- });
- <input type='button' value='员工吃饭' onclick='emp.eat()'>
五、Ext.EventObject示例
- var btn = Ext.get('buttonTest');
- btn.addListener('click',handler);
- function handler(e){
-
- var x = e.getPageX();
-
- var y = e.getPageY();
- var msg = '事件发生坐标 : x='+x+' y='+y;
- alert(Ext.EventManager.getPageXY(e));
- alert(msg);
- }
六、Ext.EventManager使用
- Ext.EventManager.addListener('btn','click',handler);
这篇关于EXT事件绑定,父级回调子级计算结果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!