zoukankan      html  css  js  c++  java
  • 【dojo】 on/emit subscribe/publish


    最近dojo evneted的两个概念一直没搞清楚,跟同事打赌,我自己肯定能查得到,结果,看了其他人的代码加上官网翻了一遍,算是有点理解了。
    on/emit 
     
    emit,某个对象发出事件。
     
    ArgumentTypeDescription
    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 an on() method.
    type String This is the name of the event type to be dispatched (like select). This event may be a standard event (like click) or a custom event (like finished).
    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 calling event.preventDefault(). The emit method does not perform any default action, it returns a value allowing the calling code to perform any default action.
     
    on是订阅指定某个对象的消息。
     
    NameTypeDescription
    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 
     
    publish 发出一个事件。
    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, "click", 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, "click", 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>

      
     
     参考:
     
     
     
     
     
  • 相关阅读:
    百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈
    百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈
    硬件笔试面试题
    硬件笔试面试题
    硬件笔试面试题
    hadoop生态搭建(3节点)-01.基础配置
    hadoop生态搭建(3节点)-01.基础配置
    Java Web开发中路径问题小结
    JavaScript 对象分类
    JavaScript 对象分类
  • 原文地址:https://www.cnblogs.com/nishilunhui/p/2987994.html
Copyright © 2011-2022 走看看