【dojo】 on/emit subscribe/publish
最近dojo evneted的两个概念一直没搞清楚,跟同事打赌,我自己肯定能查得到,结果,看了其他人的代码加上官网翻了一遍,算是有点理解了。
on/emit
emit,某个对象发出事件。
on是订阅指定某个对象的消息。
比如自定义一个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/
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |