Skip to content

Commit

Permalink
#4 Loading socket.io lib async when CRUDr is called
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Jun 21, 2012
1 parent e30ba24 commit 368ed41
Showing 1 changed file with 20 additions and 89 deletions.
109 changes: 20 additions & 89 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
// load dependencies
// - the socket.io lib
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = false;
po.src = '{{socket.io.js}}';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();

(function () {


this.CRUDr = function ( key ){

this.key = key;

//this.url = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname
//this.authorize = this.url
//this.redirect =

this.init();


// load dependencies (place in seperate method)
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = '{{socket.io.js}}';
po.onreadystatechange = this.init;
po.onload = this.init;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);

return this;
}

CRUDr.prototype = {
init : function(){

this.socket = io.connect();

// get the token...
var cookie = getCookie("access_token");
var query = getQuery("access_token");
Expand All @@ -50,50 +50,19 @@
alert("CRUDr authentication failed");
}

}
}
},
sync : function(socket, req) {

}).call(this);

(function() {
var socket = io.connect();
//var origSync = Backbone.sync;
var socket = {};
var origSync = {};

window.sync = function(method, model, options) {
var backend = model.backend || (model.collection && model.collection.backend);

var error = options.error || function() {};
var success = options.success || function() {};

// Don't pass the callbacks to the backend
delete options.error;
delete options.success;

if (backend) {
// Use Socket.IO backend
backend.ready(function() {
var req = {
method: method,
model: model.toJSON(),
options: options
};

backend.socket.emit('sync', req, function(err, resp) {
socket.emit('sync', req, function(err, resp) {
if (err) {
error(err);
} else {
success(resp);
}
});
});
} else {
// Call the original Backbone.sync
//origSync(method, model, options);
}
};

}
}

var Promise = function(obj) {
var args = null;
var callbacks = [];
Expand Down Expand Up @@ -163,47 +132,9 @@

return backend;
};

var Helpers = {
// Backbone.js support
// Listen for backend notifications and update the
// collection models accordingly.
bindBackend: function() {
var self = this;
var idAttribute = this.model.prototype.idAttribute;

this.backend.ready(function() {
var event = self.backend.options.event;

self.bind(event + ':create', function(model) {
self.add(model);
});
self.bind(event + ':update', function(model) {
var item = self.get(model[idAttribute]);
if (item) item.set(model);
});
self.bind(event + ':delete', function(model) {
self.remove(model[idAttribute]);
});
});
}
};
/*
Backbone.Collection = (function(Parent) {
// Override the parent constructor
var Child = function() {
if (this.backend) {
this.backend = buildBackend(this);
}
Parent.apply(this, arguments);
};
// Inherit everything else from the parent
return inherit(Parent, Child, [Helpers]);
})(Backbone.Collection);
*/
})();


}).call(this);

// helpers
function getQuery(name) {
Expand Down

0 comments on commit 368ed41

Please sign in to comment.