ExtJs6.0添加事件

老早就想有自己的博客,虽说作为一个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了

 

转载于:https://www.cnblogs.com/lbyjj/p/7910099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值