扩展一个button

 1 if(!dojo._hasResource["eventTest.dataPd"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
 2 dojo._hasResource["eventTest.dataPd"= true;
 3 dojo.provide("eventTest.dataPd");
 4 dojo.require("dijit.form.Button");
 5 dojo.require("eventTest.accept");
 6 dojo.declare('eventTest.dataPd', [dijit._Widget, dijit._Templated], {
 7     widgetsInTemplate: true,
 8     templateString:'<div><button dojoType=dijit.form.Button dojoAttachEvent="onClick: _onClick">Click</button></div>',
 9     oddEvent:function(){
10         for(var i=0;i<10;i++){
11             if(i%2==0)
12             {
13                 dojo.publish("oddEvents",[i]); //如果判断i为偶数,这发布一个事件,传递的参数为此时i的值
14             }
15         }
16     } ,
17     _onClick:function(){
18           var myObject=new eventTest.accept();
19           dojo.subscribe("oddEvents", myObject,'eventAccept');//点击onClik时订阅这个事件(只需订阅一次即可)即每当oddEvents事件被发布时,即调用myObject对象中的eventAccept函数,通常情况下eventAccept函数应该有一个参数用来接收i
20           this.oddEvent();
21     }
22 });
23     }

eventTest.accept类定义如下:

 1 if(!dojo._hasResource["eventTest.accept"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
 2 dojo._hasResource["eventTest.accept"= true;
 3 dojo.provide("eventTest.accept");
 4 dojo.declare('eventTest.accept',null, {
 5         constructor:function(){
 6           } ,
 7     eventAccept:function(i){
 8           console.debug(i);
 9     }
10 });
11     }

在html中:

<div dojoType="eventTest.dataPd"></div>

运行此html
出现一个button 点击 查看console输出
0
2
4
6
8

相关文章:

  • 2021-05-08
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2021-07-17
  • 2021-08-08
  • 2021-11-23
  • 2022-12-23
猜你喜欢
  • 2021-04-11
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
相关资源
相似解决方案