title: Overwriting Backbone.sync for socket.io
body: <p>Im working on a socket.io based server/client connection instead of ajax.
  Client uses Backbone and I overwritten the Backbone.sync function with one 
  <em>half assed</em> of my own:</p>
  
  <pre><code>  Backbone.sync = function (method, collection, options) {
      // use the window.io variable that was attached on init 
      var socket = window.io.connect('http://localhost:3000');
      // emit the collection/model data with standard ajax method names and options
      socket.emit(method,{collection:collection.name,url:collection.url});
      // create a model in the collection for each frame coming in through that connection 
      socket.on(collection.url,function(socket_frame){
        collection.create(socket_frame['model']);
      })
    };
  </code></pre>
  
  <p>Instead of ajax calls I simply emit through socket attached to window.io
  global var. Server listens to those emits and based on the model url, I don't want to change that behaviour and I use the default crud method names (read,patch...) inside each emited frame. The logic behind it (its a bit far thought, but who knows) that in case the client doesn't support Websockets I can easily fallback to default jQuery ajax. I attached the orginal Backbone.sync to a var so I can pass the same arguments to it when no websocket is available. </p>
  
  <p>All it that greatness behalves properly and the server answers to the client events. The server emits then each model data as a seperate websocket frames in one connection.
  I see the frames in the Network/Websocket filter as one (concurrent/established) connection 
  and things seems to be working</p>
  
  <p>Currently the function assumes I pass a collection and not a model.</p>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li><p>Is that approach ok with you?</p></li>
  <li><p>How can I use the socket.io callbacks on 'success' and 'failure' etc in Backbone the right way so I don't have to call the collection.create function 'by-hand'?</p></li>
  <li><p>Is it better to establish different concurrent connections for models/collections or use the one already established instead?</p></li>
  </ul>