vendredi 19 septembre 2014

Understanding socket service in yeoman's fullstack


Vote count:

0




I had a bit of trouble understanding the socket service that yeoman's fullstack provides. Especially the first lines of the function. What "to DI" means and how am I supposed to do it ? Also, I provided an example of how I use the syncUpdate function, adding the modelPlace variable to know from where the function has been called.


So, here is a pseudo working-space model



var working_spaceSchema = new Schema({
...
users: [],
todos: [{name:String,info:String,priority:Number,id:Number}],
chat: [{content:String,poster:String,creation:Date}],
...
});


and the way I call the socket service in my angular controllers:



$http.get('/api/works/' + $location.$$path.substr(7))
.success(function(data) {
$http.post('/api/works/' + $location.$$path.substr(7) + '/connexion', {type:0});
$scope.chats = data;
socket.syncUpdates('work', 'chat', $scope.chats, function(event, chat, chats) {
$scope.chats = chats;
});
});


For reminder, here is the socket service file. You can see the modelPlace variable, which enables me to know what I have to sync from the schema.post :



'use strict';

angular.module('yoyoApp')
.factory('socket', function(socketFactory) {

// socket.io now auto-configures its connection when we ommit a connection url
var ioSocket = io(null, {
// Send auth token on connection, you will need to DI the Auth service above
// 'query': 'token=' + Auth.getToken()
});

var socket = socketFactory({
ioSocket: ioSocket
});

return {
socket: socket,

/**
* Register listeners to sync an array with updates on a model
*
* Takes the array we want to sync, the model name that socket updates are sent from,
* and an optional callback function after new items are updated.
*
* @param {String} modelName
* @param {Array} array
* @param {Function} cb
*/


// Should consider givin' $state.params.id to syncUpdates
// check currentUser.state and trigger notification IF =/= from update current state
syncUpdates: function (modelName, modelPlace, array, cb) {
cb = cb || angular.noop;

/**
* Syncs item creation/updates on 'model:save'
*/

socket.on(modelName + ':save', function (item) {
var event = 'created';
if (modelPlace == 'todo_list')
array = item.todos;
else if (modelPlace == 'chat')
array = item.chat;

cb(event, item, array);

});

/**
* Syncs removed items on 'model:remove'
* JE CROIS QUE CE N'EST PAS NECESSAIRE
*/
socket.on(modelName + ':remove', function (item) {
var event = 'deleted';
_.remove(array, {_id: item._id});
cb(event, item, array);
});
},

/**
* Removes listeners for a models updates on the socket
*
* @param modelName
*/
unsyncUpdates: function (modelName, modelPlace) {
socket.removeAllListeners(modelName + ':save:' + modelPlace);
socket.removeAllListeners(modelName + ':remove:' + modelPlace);
}
};
});


Added to the "ioSocket variable" question, I would like to know how I should use the syncUpdate function in regard of what the best practice is (mine isn't, obviously...).


Thank you guys !



asked 54 secs ago







Understanding socket service in yeoman's fullstack

Aucun commentaire:

Enregistrer un commentaire