2017-04-24 58 views
1

我使用PHP推送庫(angularjs)與PHP後端。推送解除綁定不起作用

JS:

// Enable pusher logging - don't include this in production 
Pusher.logToConsole = true; 

var pusher = new Pusher('API_KEY', { 
     cluster: 'ap2', 
     encrypted: true 
}); 
var channel = pusher.subscribe('my-channel'); 
channel.bind('my-event', function (data) { 
    common.flashMsg('success', data.message); 
}); 

的綁定和取消綁定代碼:

$scope.workerStatus = function (status) { 
     if (status == 'available') { 
      common.flashMsg('success', "Welcome Again...!"); 
      channel.bind('my-event', function (data) { 
       common.flashMsg('success', data.message); 
      }); 
     } else { 
      common.flashMsg("error", "You're now offline. Good Bye."); 
      channel.unbind('my-event', function() { 
       common.flashMsg('success', data.message); 
      }); 
     } 
} 

我想取消綁定channel,當用戶註銷,並停止通知,但渠道unbind事件不能正常工作,用戶仍能夠收到通知。 我錯過了什麼?

+0

我經過的引用,不同的功能,因此呼叫失敗,現在問題同樣的功能解決了第二個參數'unbind' –

回答

-1

我認爲你遇到問題的原因是你正在將一個函數傳遞給unbind調用。嘗試省略第二個參數以刪除該事件的所有偵聽器。如果您擁有多個處理程序,則傳入事件處理程序將保留爲解除綁定該特定處理程序。如果您將多個事件處理程序綁定到事件並僅想解除其中一個事件處理程序,那麼這非常有用。

查看更多在這裏:https://github.com/pusher/pusher-js#bind-and-unbind