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

时间:2015-06-18 09:14:55

标签: javascript deezer

在Deezer Javascript SDK中,DZ.Event.unsubscribe方法可以用于unsubscribe all subscriptions for a specific event,但有没有办法取消订阅特定订阅而不是所有订阅?

DZ.Event.subscribe方法不返回可以在取消订阅调用中给出的任何ID,并且将订阅回调函数发送到unsubscription方法也不起作用:

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

1 个答案:

答案 0 :(得分:1)

目前只有讨厌的方法可以访问DZ.Event.callbacks

例如你可以:

//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
        }
    }
}