BackboneJS - 事件 stopListening



說明

正如其名稱所指定的那樣,它可用於停止偵聽其他物件上的事件。

語法

object.stopListening(other, event, callback)

引數

  • other − 定義其他物件的名稱。

  • event − 繫結一個物件。

  • callback − 引用程式碼,並以物件作為上下呼叫。

示例

<!DOCTYPE html>
<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 TutorialsPoint..'}, 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.htm 檔案中。

  • 在瀏覽器中開啟此 HTML 檔案。

backbonejs_events.htm
廣告