0eb15d449c4b4bc0d49158a92d25350b5391fa4e
[motion.git] / server / api / motionbar / motionbar.socket.js
1 /**
2  * Broadcast updates to client when the model changes
3  */
4
5 'use strict';
6
7 var Motionbar = require('../../models').Motionbar;
8
9 exports.register = function(socket) {
10   Motionbar.afterCreate(function(doc) {
11     onSave(socket, doc);
12   });
13   Motionbar.afterDestroy(function(doc) {
14     onRemove(socket, doc);
15   });
16 }
17
18 function onSave(socket, doc, cb) {
19   // doc.UserId is string. You don't use ===
20   if (socket.userId == doc.UserId) {
21     socket.emit('motionbar:save', doc);
22   }
23 }
24
25 function onRemove(socket, doc, cb) {
26   socket.emit('motionbar:remove', doc);
27 }