老早就想有自己的博客,虽说作为一个It入门的新人,写的博客没太大技术含量,多数是从网上找资料,查度娘。仅此作为一个自己的学习笔记用
解决:Ext自定义事件
在学习一个PDF的文档中找到一个Extjs的自定义事件练习,跟着文档代码走的时候,怎么也实现不了功能,先贴上PDF书中的代码
1 <script> 2 Ext.onReady(function () { 3 //创建一个类 4 var Person = Ext.extend(Ext.util.Observable, { 5 constructor: function (name) { 6 this.name = name; 7 this.sayNum = 0; 8 this.say = function () { 9 if (this.sayNum < 2) { 10 this.sayNum += 1; 11 alert("我是" + this.name + "这是点击第" + this.sayNum); 12 } 13 else { 14 this.sayNum = 0;//触发自定义事件后sayNum归为0 15 this.fireEvent('onSay', this);//激发自定义事件 16 } 17 } 18 this.addEvents({ 19 //加入自定义事件 20 "onSay":true 21 }); 22 } 23 }); 24 var per = new Person("晶晶");//创建对象 25 //为自定义事件绑定处理函数 26 per.addListener('onSay', function handler() { 27 alert("发生了自定义事件"); 28 }); 29 }); 30 </script>
----body下的html元素
<input type="button" value="ssss" οnclick="per.say()" />
然后就一直报错
具体报错原因不知道,事后发现,
per.addListener('onSay', function handler() {
alert("发生了自定义事件");
});
这行代码中的addListener添加事件方法在ExtJs6.0的API中不存在,找到百度后从新做了个练习
1 <script> 2 Ext.onReady(function () { 3 var Person = Ext.extend(Ext.util.Observable, { 4 constructor: function (config) { 5 this.name = config.name; 6 this.sayNum = config.sayNum; 7 this.listeners = config.listeners; 8 Person.superclass.constructor.call(this, config); 9 this.say = function () { 10 if (config.sayNum < 2) { 11 config.sayNum += 1; 12 alert("哈哈哈哈我是" + config.name + "这是第" + config.sayNum + "次"); 13 } 14 else { 15 config.sayNum = 0; 16 this.fireEvent('onSay', this); 17 } 18 } 19 } 20 }); 21 var per = new Person({ 22 name: '晶晶', 23 sayNum: 0, 24 listeners: { 25 "onSay": function () { 26 alert("自定义事件能不能成?"); 27 } 28 } 29 }); 30 Ext.get('fireBtn').on('click', function () { 31 per.say(); 32 });
}); 54 </script>
<body>
<input type="button" value="say" id="fireBtn"/>
</body>
实现就是这样实现的,但是说不出所以然,继续去搞ExtJs了