Created
May 3, 2012 04:23
-
-
Save kane-thornwyrd/2583155 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.sync = (method, model, options = null)-> | |
socket = window.NAMESPACE.socket | |
@signature = (model)-> | |
sig = {} | |
sig.endPoint = model.url + (('/' + model.id) if model.id else '') | |
sig.ctx = model.ctx if model.ctx | |
sig | |
@event = (operation, sig)-> | |
if sig.ctx | |
"#{operation}:#{sig.endPoint}:#{sig.ctx}" | |
else | |
"#{operation}:#{sig.endPoint}" | |
@create = (model)-> | |
sign = signature model | |
e = event 'create', sign | |
socket.emit 'create', | |
'signature' : sign | |
item : model.attributes | |
socket.once e, (data)-> | |
model.id = data.id | |
console.log model | |
switch method | |
when 'create' | |
@create model | |
when 'read' | |
@read model | |
when 'update' | |
@update model | |
when 'delete' | |
@destroy model | |
#WIP |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.sync = function (method, model, options) { | |
var socket = window.NAMESPACE.socket; // grab active socket from global namespace; io.connect() was used to create socket | |
/* | |
* Create signature object that will emitted to server with every request. | |
* This is used on the server to push an event back to the client listener. | |
*/ | |
var signature = function (model) { | |
var sig = {}; | |
sig.endPoint = model.url + (model.id ? ('/' + model.id) : ''); | |
if (model.ctx) sig.ctx = model.ctx; | |
return sig; | |
}; | |
/* | |
* Create an event listener for server push. The server notifies | |
* the client upon success of CRUD operation. | |
*/ | |
var event = function (operation, sig) { | |
var e = operation + ':'; | |
e += sig.endPoint; | |
if (sig.ctx) e += (':' + sig.ctx); | |
return e; | |
}; | |
// Save a new model to the server. | |
var create = function (model) { | |
var sign = signature(model); | |
var e = event('create', sign); | |
socket.emit('create', {'signature' : sign, item : model.attributes }); | |
socket.once(e, function (data) { | |
model.id = data.id; | |
console.log(model); | |
}); | |
}; | |
// Get a collection or model from the server. | |
var read = function (model) { | |
var sign = signature(model); | |
var e = event('read', sign); | |
socket.emit('read', {'signature' : sign}); | |
socket.once(e, function (data) { | |
options.success(data); // updates collection, model; fetch | |
}); | |
}; | |
// Save an existing model to the server. | |
var update = function (model) { | |
var sign = signature(model); | |
var e = event('update', sign); | |
socket.emit('update', {'signature' : sign, item : model.attributes }); // model.attribues is the model data | |
socket.once(e, function (data) { | |
console.log(data); | |
}); | |
}; | |
// Delete a model on the server. | |
var destroy = function (model) { | |
var sign = signature(model); | |
var e = event('delete', sign); | |
socket.emit('delete', {'signature' : sign, item : model.attributes }); // model.attribues is the model data | |
socket.once(e, function (data) { | |
console.log(data); | |
}); | |
}; | |
// entry point for method | |
switch (method) { | |
case 'create': | |
create(model); | |
break; | |
case 'read': | |
read(model); | |
break; | |
case 'update': | |
update(model); | |
break; | |
case 'delete': | |
destroy(model); | |
break; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This is a stub for a socket.io server that responds to CRUD operations | |
*/ | |
var io = require('socket.io').listen(3000); | |
var create = function (socket, signature) { | |
var data = []; | |
var e = event('create', signature); | |
socket.emit(e, {id : 1}); | |
}; | |
var read = function (socket, signature) { | |
var data = []; | |
var e = event('read', signature); | |
data.push({}) | |
socket.emit(e, data); | |
}; | |
var update = function (socket, signature) { | |
var data = []; | |
var e = event('update', signature); | |
socket.emit(e, {success : true}); | |
}; | |
var destroy = function (socket, signature) { | |
var data = []; | |
var e = event('delete', signature); | |
socket.emit(e, {success : true}); | |
}; | |
// creates the event to push to listening clients | |
var event = function (operation, sig) { | |
var e = operation + ':'; | |
e += sig.endPoint; | |
if (sig.ctx) e += (':' + sig.ctx); | |
return e; | |
}; | |
io.sockets.on('connection', function (socket) { | |
socket.on('create', function (data) { | |
create(socket, data.signature); | |
}); | |
socket.on('read', function (data) { | |
read(socket, data.signature); | |
}); | |
socket.on('update', function (data) { | |
update(socket, data.signature); | |
}); | |
socket.on('delete', function (data) { | |
destroy(socket, data.signature); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The more I read it, the more I find errors, correction incoming today !