加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【dojo】 on/emit subscribe/publish

发布时间:2020-12-16 21:59:04 所属栏目:百科 来源:网络整理
导读:最近dojo evneted的两个概念一直没搞清楚,跟同事打赌,我自己肯定能查得到,结果,看了其他人的代码加上官网翻了一遍,算是有点理解了。 on/emit emit,某个对象发出事件。 Argument Type Description target Object This is the target object (a DOM node

最近dojo evneted的两个概念一直没搞清楚,跟同事打赌,我自己肯定能查得到,结果,看了其他人的代码加上官网翻了一遍,算是有点理解了。
on/emit
emit,某个对象发出事件。
Argument Type Description
target Object This is the target object (a DOM node or other event emitting object) that will be the source of the event. The target object may be a host object with its own event capabilities (like DOM elements or the window),or it may be a JavaScript object with anon()method.
type String This is the name of the event type to be dispatched (likeselect). This event may be a standard event (likeclick) or a custom event (likefinished).
event Object

This is an object with the properties of the event to be dispatched. Generally you should align your properties with W3C standards. Two properties are of particular importance:

  • bubbles - This indicates that the event should bubble up,first firing on the target object,next on the target object's parent (parentNode) and so on until it reaches the top of the DOM or bubbling is stopped. Bubbling is stopped when a listener callsevent.stopPropagation().
  • cancelable - This indicates that the event's default action can be cancelled. The default action is cancelled by a listener by callingevent.preventDefault(). The emit method does not perform any default action,it returns a value allowing the calling code to perform any default action.
on是订阅指定某个对象的消息。
Name Type Description
target Element|Object

This is the target object or DOM element that to receive events from

type String|Function

This is the name of the event to listen for or an extension event type.

listener Function

This is the function that should be called when the event fires.

dontFix undefined
比如自定义一个widget,创建按钮点击事件为_onStopRender
<button data-dojo-type="dijit/form/Button" type="submit" data-dojo-attach-point = "createJob"data-dojo-attach-event="onClick:      _onStopRender" >创建</button>
---------------------------------------------------------
widget部分发布一个createJob的事件:
_onStopRender : function(){
this.emit("createJob",{});

}
----------------------------------------------
UI部分new了widget之后,去订阅它的事件:
var iDialog= new JobDialog().placeAt("createJobProperties");
on(iDialog,"createJob",function(){
alert(0);
});
subscribe/publish
pub lish 发出一个事件。
subscribe用于匿名事件,不晓得到底是哪个对象发出的。(胆儿够肥的,匿名也接。。。)
官网例子:

<button id="alertButton">Alert the user</button>

<button id="createAlert">Create another alert button</button>

----------------------------------------------------------------------------

<script>

require(["dojo/on","dojo/topic","dojo/dom-construct","dojo/dom","dojo/domReady!"],

function(on,topic,domConstruct,dom) {

var alertButton = dom.byId("alertButton"),

createAlert = dom.byId("createAlert");

    on(alertButton,"click",function() {

// When this button is clicked,

// publish to the "alertUser" topic

topic.publish("alertUser","I am alerting you.");

});

on(createAlert,function(evt){

// Create another button

var anotherButton = domConstruct.create("button",{

innerHTML: "Another alert button"

},createAlert,"after");

// When the other button is clicked,

// publish to the "alertUser" topic

on(anotherButton,function(evt){

topic.publish("alertUser","I am also alerting you.");

});

});

// Register the alerting routine with the "alertUser" topic.

topic.subscribe("alertUser",function(text){

alert(text);

});

});

</script>

参考:
http://livedocs.dojotoolkit.org/dojo/on
http://dojotoolkit.org/documentation/tutorials/1.7/events/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读