【dojo】 on/emit subscribe/publish

前端之家收集整理的这篇文章主要介绍了【dojo】 on/emit subscribe/publish前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近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:@H_403_60@

  • 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@H_403_60@

type String|Function

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

listener Function

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

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>@H_403_60@

----------------------------------------------------------------------------@H_403_60@

<script>@H_403_60@

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

function(on,topic,domConstruct,dom) {@H_403_60@

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

createAlert = dom.byId("createAlert");@H_403_60@

@H_403_60@

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

// When this button is clicked,@H_403_60@

// publish to the "alertUser" topic@H_403_60@

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

});@H_403_60@

on(createAlert,function(evt){@H_403_60@

// Create another button@H_403_60@

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

innerHTML: "Another alert button"@H_403_60@

},createAlert,"after");@H_403_60@

// When the other button is clicked,@H_403_60@

// publish to the "alertUser" topic@H_403_60@

on(anotherButton,function(evt){@H_403_60@

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

});@H_403_60@

});@H_403_60@

@H_403_60@

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

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

alert(text);@H_403_60@

});@H_403_60@

});@H_403_60@

</script>@H_403_60@

参考:
原文链接:/dojo/290927.html

猜你在找的Dojo相关文章