Skip to content

Commit

Permalink
starting to mess around with integrating with thoonk
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJoreteg committed Jun 28, 2011
1 parent 424e250 commit 884f507
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions capsule.js
Expand Up @@ -7,11 +7,15 @@
var Capsule, var Capsule,
Backbone, Backbone,
_, _,
uuid; uuid,
Thoonk,
thoonk;


if (typeof exports !== 'undefined') { if (typeof exports !== 'undefined') {
Backbone = require('backbone'); Backbone = require('backbone');
_ = require('underscore')._; _ = require('underscore')._;
Thoonk = require('thoonk').Thoonk;
thoonk = new Thoonk();
uuid = require('node-uuid'); uuid = require('node-uuid');
Capsule = exports; Capsule = exports;
} else { } else {
Expand Down Expand Up @@ -126,17 +130,13 @@
// ###deleteServer // ###deleteServer
// Sends delete event for `id` to server. // Sends delete event for `id` to server.
deleteServer: function () { deleteServer: function () {
socket.send({ socket.emit('delete', { id: this.id });
event: 'delete',
id: this.id
});
}, },


// ###callServerMethod // ###callServerMethod
// Send a method call event. To trigger a model method on the server (if allowed). // Send a method call event. To trigger a model method on the server (if allowed).
callServerMethod: function (method) { callServerMethod: function (method) {
socket.send({ socket.emit('method call', {
event: 'method',
id: this.id, id: this.id,
method: method method: method
}); });
Expand Down Expand Up @@ -313,8 +313,7 @@
// ###setServer // ###setServer
// Our server version of the normal `set` method. Takes a hash of attributes // Our server version of the normal `set` method. Takes a hash of attributes
setServer: function(attrs) { setServer: function(attrs) {
socket.send({ socket.emit('set', {
event: 'set',
id: this.id, id: this.id,
change: attrs change: attrs
}); });
Expand All @@ -323,8 +322,7 @@
// ###unsetServer // ###unsetServer
// Unsets a given property // Unsets a given property
unsetServer: function(property) { unsetServer: function(property) {
socket.send({ socket.emit('unset', {
event: 'unset',
id: this.id, id: this.id,
property: property property: property
}); });
Expand Down Expand Up @@ -352,7 +350,27 @@
// ###register // ###register
// Generates an `id` if on server and sets it in our reference hash. // Generates an `id` if on server and sets it in our reference hash.
register: function () { register: function () {
if (Capsule.server) this.id = uuid(); if (Capsule.server) {
var self = this;
this.id = uuid();
this.feed = thoonk.create(this.type + ':' + this.type, {max_length: this.maxLength});
this.feed.subscribe(
function (id, item) {
// add
console.log('args and this', arguments, this, this === self.feed);
self.feed();
},
function () {
// edit
},
function () {
// retract
},
function () {
// move
}
);
}
if (this.id && !Capsule.models[this.id]) Capsule.models[this.id] = this; if (this.id && !Capsule.models[this.id]) Capsule.models[this.id] = this;
}, },


Expand All @@ -374,8 +392,7 @@
// ###addServer // ###addServer
// The server version of backbone's `add` method. // The server version of backbone's `add` method.
addServer: function (data) { addServer: function (data) {
socket.send({ socket.emit('add', {
event: 'add',
id: this.id, id: this.id,
data: data data: data
}); });
Expand All @@ -384,8 +401,7 @@
// ###moveServer // ###moveServer
// Send the `move` event // Send the `move` event
moveServer: function (id, newPosition) { moveServer: function (id, newPosition) {
socket.send({ socket.emit('move', {
event: 'move',
collection: this.id, collection: this.id,
id: id, id: id,
newPosition: newPosition newPosition: newPosition
Expand Down

0 comments on commit 884f507

Please sign in to comment.