简体   繁体   English

使用Deezer Javascript SDK DZ.Event.unsubscribe取消特定订阅

[英]Unsubscribe specific subscription with Deezer Javascript SDK DZ.Event.unsubscribe

In the Deezer Javascript SDK, the DZ.Event.unsubscribe method can be used to unsubscribe all subscriptions for a specific event , but is there any way to unsubscribe a specific subscription instead of all of them? 在Deezer Javascript SDK中, DZ.Event.unsubscribe方法可用于取消订阅特定事件的所有订阅 ,但是有什么方法可以取消订阅特定订阅而不是全部订阅?

The DZ.Event.subscribe method doesn't return any ID that could be given in the unsubscription call, and sending the subscription callback function to the unsubscription method doesn't work either: DZ.Event.subscribe方法不会返回取消订阅调用中可能提供的任何ID,并且将订阅回调函数发送到取消订阅方法也不起作用:

function callback(args) {
  console.log('GOT player_position 1', args);
}
DZ.Event.subscribe('player_position', callback);
DZ.Event.subscribe('player_position', args => {
  console.log('GOT player_position 2', args);
});
DZ.Event.unsubscribe('player_position', callback); // Unsubscribes both subscriptions

There are currently only nasty ways to do this with access to DZ.Event.callbacks 当前只有访问DZ.Event.callbacks 讨厌方法

For instance you can: 例如,您可以:

//declare your own unsubscribe function
var unsubscribe = (event, callback) => {
    if(DZ.Event.callbacks[event]) {
        let index = DZ.Event.callbacks[event].indexOf(callback); //find the index of your callback
        if(index !== -1) {
            DZ.Event.callbacks[event].splice(index, 1); //remove it
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM