1、自定义类(SomeBody):
SomeBody = function(name){ this.name = name; this.addEvents("play","eat");}Ext.extend(SomeBody,Ext.util.Observable);
2、调用上面的自定义类(SomeBody),给实例添加EventListener
var xiaoyang = new SomeBody("xiaoyang"); xiaoyang.on("play",function(location){ Ext.Msg.alert("event",xiaoyang.name+ "在打"+ location+"!"); }); xiaoyang.on("eat",function(){ Ext.Msg.alert("event",xiaoyang.name+ "在吃东西!"); });
3、将页面上的元素的事件与xiaoyang这个SomeBody实例的相关事件绑定:
Ext.get("play").on("click",function(){ xiaoyang.fireEvent("play","羽毛球");}); Ext.get("eat").on("click",function(){ xiaoyang.fireEvent("eat");});get方法中的play,eat为页面中的元素;Ext API中的解释:The id of the node, a DOM Node or an existing Element;