Skip to content

Instantly share code, notes, and snippets.

@ckniffen
Created November 14, 2012 21:45
Show Gist options
  • Save ckniffen/4075052 to your computer and use it in GitHub Desktop.
Save ckniffen/4075052 to your computer and use it in GitHub Desktop.
Backbone socket.io framework rough draft currently code named Nerve.io
define([
'lodash',
'backbone'
], function(_, Backbone){
var Nerve = {
ioClient: null,
init: function(ioClient){
Nerve.ioClient = ioClient;
}
};
Nerve.Client = function(){
};
_.extend(Nerve.Client.prototype, {
listenFor: function(collection, prefix){
var _this = this;
var io = Nerve.ioClient;
_.bindAll(this);
var eventDecorator = function(fn, collection){
return function(data){
fn.call(_this, data, collection);
};
};
io.on(prefix + ":create", eventDecorator(this.onCreate, collection));
io.on(prefix + ":update", eventDecorator(this.onUpdate, collection));
io.on(prefix + ":destroy", eventDecorator(this.onDestroy, collection));
},
onCreate: function(data, collection){
console.log('create', arguments);
collection.add(data);
},
onUpdate: function(data, collection){
console.log('update', arguments);
var model = new collection.model(data);
collection.get(model.id).set(data);
},
onDestroy: function(id, collection){
console.log('destroy', arguments);
var model = collection.get(id);
if(model){ collection.remove(model); }
}
});
// Add socket ID to every Backbone.sync request
var origBackboneSync = Backbone.sync;
Nerve.sync = function(method, model, options) {
options.headers = _.extend(
{ 'X-Socket-ID': Nerve.ioClient.socket.sessionid },
options.headers
);
return origBackboneSync(method, model, options);
};
return Nerve;
});
/*jshint node:true */
/*global require */
"use strict";
var _ = require('underscore'),
EventEmitter = require('events').EventEmitter;
var Nerve = {};
exports = module.exports = Nerve;
Nerve.Broadcaster = function(sockets, namespace){
this.sockets = sockets;
this.namespace = namespace || '';
};
_.extend(Nerve.Broadcaster.prototype, {
broadcast: function(eventName, socketId, data){
var prefix = (this.namespace) ? this.namespace + ':' : '';
this.sockets.except(socketId).emit(prefix + eventName, data);
},
onCreate: function(data, socketId, modelPrefix){
this.broadcast(modelPrefix + ':create', socketId, data);
},
onUpdate: function(data, socketId, modelPrefix){
this.broadcast(modelPrefix + ':update', socketId, data);
},
onDestroy: function(data, socketId, modelPrefix){
this.broadcast(modelPrefix + ':destroy', socketId, data);
},
listenTo: function(modelController, modelPrefix){
var _this = this;
var eventDecorator = function(fn, modelPrefix){
return function(data, request){
var socketId = request.header('X-Socket-ID');
fn.call(_this, data, socketId, modelPrefix);
};
};
modelController.on('create', eventDecorator(this.onCreate, modelPrefix));
modelController.on('update', eventDecorator(this.onUpdate, modelPrefix));
modelController.on('destroy', eventDecorator(this.onDestroy, modelPrefix));
}
});
Nerve.ServerCollection = function(prefix){
this.prefix = prefix;
};
_.extend(Nerve.ServerCollection.prototype, EventEmitter.prototype, {
addRoutes: function(app){
var prefix = '/' + this.prefix;
_.bindAll(this);
app.get(prefix + '/:id', this.readOne);
app.get(prefix, this.read);
app.post(prefix, this.create);
app.put(prefix + '/:id', this.update);
app.delete(prefix + '/:id', this.destroy);
},
create: function(req, res){
},
update: function(req, res){
},
destroy: function(req, res){
},
read: function(req, res){
},
readOne: function(req, res){
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment