angular-restmod
angular-restmod copied to clipboard
socket.io mixin - OR how to create a mixin which modifies to a collection?
I am attempting to create a mixin that will do the following to make any model "socket" compatible:
- Decode and add new item to collection
- Delete from collection
- Update from collection
- Work automatically when when using
Trip.find(1)andTrip.$search()andTrip.events.$fetch()
I'm using code that looks something like this:
//socket api sends messages in the following format:
{
model: "Trip",
verb: "create",
data: {tripId: 2, name: 'my epic trip!'}
}
// pseudo code
return restmod.mixin(function() {
this.define('Scope.$socket', function() {
var Scope = this;
var model = Scope.$type;
var sock = new WebSocket('ws://sockethost:9999/trips');
sock.onmessage = function(event) {
var message = event.data || {};
if (message.model === 'Trip') {
switch (message.verb) {
case 'create':
console.log('CREATE -> add to collection');
break;
case 'destroy':
console.log('DELETE -> remove from collection');
break;
case 'update':
console.log('UPDATE -> update item in collection');
break;
}
}
};
});
});
Any suggestions or ideas around this?
Just an update on this - I've created a pretty mean socket mixin... I will post the code soon.
It mixes into any model and exposes a $subscribe method.
nice!, looking forward to see it!
@facultymatt - did you ever post this mixin anywhere? I'm keen to check it out!