relayEvents传播分发事件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link rel="stylesheet" type="text/css" href="ext-4.0.7-gpl/resources/css/ext-all.css" />
<script type="text/javascript" src="ext-4.0.7-gpl/bootstrap.js"></script>
<script type="text/javascript" src="ext-4.0.7-gpl/locale/ext-lang-zh_CN.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
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
});
// this.listeners=config.listeners;
Employee.superclass.constructor.call(this,config);
}
});
//定义服务员类
var Waiter=Ext.extend(Ext.util.Observable,{
constructor:function(config){
Waiter.superclass.constructor.call(this,config);
}
});
//创建员工对象
[color=red] window.emp=new Employee({name:'Tom'});[/color]
emp.addListener('startEat',function(){
alert(this.name+"开始吃饭了");
});
emp.addListener("finishEat",function(){
alert(this.name+"吃完饭,收拾盘子");
});
var waiter=new Waiter();
//使服务员对象与员工对象finishEat事件进行关联
//也就是说当员工对象finishEat事件发生后,先执行自身的事件监听器
//然后事件也会传递到服务员对象中,激发服务员对象的finishEat事件
[color=red]waiter.relayEvents(emp,['finishEat']);[/color]
waiter.on('finishEat',function(){
alert("服务员开始收拾");
});
});

</script>
</head>

<body>
<table>
<tr>
[color=red] <td><button type="button" id="btn" style="width:50px;height:30px;" value="ok" οnclick="window.emp.eat()" >确定</button></td>[/color]
</tr>
</table>
</body>
</html>

通过调用relayEvents方法可以实现事件在不同对象之间的传播和分发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值