发布于 2015-08-14 15:11:01 | 59 次阅读 | 评论: 0 | 来源: 网络整理

正如它的名字说明一样,它可以用来停止监听到另一对象的事件。

语法


object.stopListening(other, event, callback)

参数:

  • other:  它定义了其他对象的名称。
  • event:  它绑定对象。
  • callback:  这是参考代码,并调用对象作为上下文。

示例


<!DOCTYPE html>
  <head>
    <title>Event Once Example</title>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
  </head>
  <body>
     <script type="text/javascript">

     	//Create an object 'myVal' and 'myVal1' and extend them using Backbone.Events method
        var myVal = _.extend({name:'Hello..'}, Backbone.Events);
        var myVal1 = _.extend({name:'Welcome to YiiBai..'}, Backbone.Events);

        //created the 'listenMe' callback function and invoked when one object listens to particular event on another object
        var listenMe = function(){
           document.write("The value is: ");
           document.write(this.name);
        };

        //The object 'myVal1' listens once for the 'listenMe' event triggered on object 'myVal'
        myVal1.listenTo(myVal, 'listenMe', listenMe);

        //The 'myVal' has no 'listenMe' event and display the value of 'myVal1'
        myVal.trigger('listenMe');

        //The 'myVal1' stops listening to specific event on 'myVal' and displays nothing
        myVal1.stopListening(myVal,'listenMe');
        myVal.trigger('listenMe');
     </script>
  </body>
</html>

输出

让我们进行以下步骤来看看上面的代码工作:

  • 保存上述代码在文件stoplistening.html

  • 在浏览器中打开这个HTML文件。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务