juggernaut icon indicating copy to clipboard operation
juggernaut copied to clipboard

Singleton subscription ?

Open Sephi-Chan opened this issue 14 years ago • 0 comments

Hi,

I was wondering how to implement some kind of singleton subscription. Example:

$('body').delegate '#new_participation a', 'ajax:success', (event, data)->
  channel = "games/#{data.game.id}/joins"
  juggernaut.subscribe channel, (data)->
    console.log "Data received...", data

The problem : after each event, a new subscribtion is created and the log is called many times. To fix it:

$('body').delegate '#new_participation a', 'ajax:success', (event, data)->
  channel = "games/#{data.game.id}/joins"
  unless subscriptions[channel]
    subscriptions[channel] = true
    juggernaut.subscribe channel, (data)->
      console.log "Data received...", data
  else
    console.log "Already subscribed!"

I believe this need is really common so, do you mind if I send a pull request to add it at the library level?

Romain

Sephi-Chan avatar Oct 12 '11 22:10 Sephi-Chan