Skip to content

Instantly share code, notes, and snippets.

@robotmay
Last active December 12, 2015 12:28
Show Gist options
  • Save robotmay/4771887 to your computer and use it in GitHub Desktop.
Save robotmay/4771887 to your computer and use it in GitHub Desktop.
An in progress, broken, Pusher adapter for Ember.js
Kuiper.Pusher = (opts = {}) ->
@init(opts)
this
$.extend Kuiper.Pusher.prototype,
options: {}
activeChannels: []
init: (@options) ->
@pusher = new Pusher(@options.key)
@pusher.connection.bind 'connected', => @connected()
@store = @options.store
connected: ->
@socketID = @pusher.connection.socket_id
@addSocketIDToXHR()
addSocketIDToXHR: ->
Ember.$.ajaxPrefilter (options, originalOptions, xhr) =>
xhr.setRequestHeader 'X-Pusher-Socket', @socketID
subscribeModels: (models = []) ->
for model in models
@subscribeModel(model)
subscribeModel: (model) ->
channelName = "#{@options.channelPrefix}-#{@modelName(model)}"
channel = @pusher.subscribe(channelName)
channel.bind 'updated', (data) =>
console?.log ['updated', data]
# What happens here?
record = model.find(data[@modelName(model)].id)
@store.load(model, data)
modelName: (model) ->
parts = model.toString().split('.')
name = parts[parts.length - 1]
name.replace(/([A-Z])/g, '_$1').toLowerCase().slice(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment