使用 Deezer Javascript SDK DZ.Event.unsubscribe 退订特定订阅
Unsubscribe specific subscription with Deezer Javascript SDK DZ.Event.unsubscribe
在Deezer Javascript SDK中,DZ.Event.unsubscribe
方法可以用来unsubscribe all subscriptions for a specific event,但是有什么方法可以退订一个特定的订阅而不是全部退订吗?
DZ.Event.subscribe
方法没有return任何可以在退订调用中给出的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
目前只有 讨厌的 方法可以访问 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
}
}
}
在Deezer Javascript SDK中,DZ.Event.unsubscribe
方法可以用来unsubscribe all subscriptions for a specific event,但是有什么方法可以退订一个特定的订阅而不是全部退订吗?
DZ.Event.subscribe
方法没有return任何可以在退订调用中给出的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
目前只有 讨厌的 方法可以访问 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
}
}
}