feathersjs/canjs-feathers — pull request #13 by marshallswain (2014-12-30 22:20:42+00:00)
Title: Connect again

This PR makes `canjs-feathers` compatible with apps that use authentication, especially if realtime communication is need both BEFORE and AFTER authentication. Now it is possible, after authentication, to change the socket connection and do something like the example below. Assume a todos model as been set up, already.

```js
// Connect without authentication...
var socket = io('', {transports: ['websocket'] });
can.Feathers.connect(socket);

todos.findAll({}) // gets public todos

// Then later, with authentication...
socket = io('', {
query: 'token=' + localStorage.getItem('featherstoken'),
transports: ['websocket']
});
can.Feathers.connect(socket);

todos.findAll({}) // gets public todos and the user's private todos.
```

I've set up the imports to bring in can.Map. The connection Map stores the `socket` and also a `ready` deferred to use when switching sockets. Upon making a second connection with `can.Feathers.connect(socket)`, the previous socket is disconnected then removed from the connection Map, which triggers removal of all of the old handlers. When the new socket is set up on the connection Map, queries are deferred until all of the handlers have been set up on each resource.

I also added `can.Feathers.disconnect()`;

I tried to test this using the instructions here to work with an existing grunt project: http://gruntjs.com/getting-started, but grunt kept telling me that a gruntfile wasn't found. (Maybe I should be ashamed to admit that I've not used grunt, only gulp. :) So, although I haven't run the tests in the test folder, I haven't run into any issues.