Skip to content

Instantly share code, notes, and snippets.

@robotmay
Created February 12, 2013 11:45
Show Gist options
  • Save robotmay/4761766 to your computer and use it in GitHub Desktop.
Save robotmay/4761766 to your computer and use it in GitHub Desktop.
Ignore the crappy Ajax bit!
DS.Model.reopenClass
modelName: ->
model = this
parts = model.toString().split('.')
name = parts[parts.length - 1]
name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1)
getChannelPrefix: (callback) ->
@channelPrefix || $.get "/accounts/current.json", (data) ->
@channelPrefix = data['account']['pusher_channel']
callback(@channelPrefix)
getChannelName: (callback) ->
name = @modelName()
@getChannelPrefix (channel) ->
callback("#{channel}-#{name}")
subscribe: ->
model = this
pusher = EmberPusherInstance.get('pusher')
@getChannelName (channel) ->
channel = pusher.subscribe(channel)
channel.bind 'created', (data) ->
console?.log ['created', data]
channel.bind 'updated', (data) ->
console?.log ['updated', data]
DS.defaultStore.load(model, data)
channel.bind 'destroyed', (data) ->
console?.log ['destroyed', data]
record = model.find(data.id)
unless record.get('isDeleted')
record.unloadRecord()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment