diff --git a/lib/client.js b/lib/client.js index 2c329a1..56bfec8 100755 --- a/lib/client.js +++ b/lib/client.js @@ -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"); @@ -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 = []; @@ -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) {