2014-09-19 22 views
1

对于了解yeoman完整堆栈提供的套接字服务,我有点麻烦。我提供了一个如何使用syncUpdate函数的示例,添加了modelPlace变量以知道函数的调用方式。了解yeoman完整堆栈中的套接字服务

所以,这里是一个伪工作空间模型

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

,我叫我的角度控制器的Socket服务方式:

$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; 
      }); 
     }); 

对于提醒,这里是Socket服务文件。你可以看到modelPlace变量,它使我知道我必须从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); 
    } 
}; 
}); 

添加到“ioSocket变量”的问题,我想知道我应该如何使用syncUpdate就最好的做法是什么(我的不是,显然是......)。

谢谢你们!

回答

2

“到DI”意味着使用依赖注入来获取Auth服务。Auth是Angular-Fullstack提供的另一种服务。只需在其中一个控制器的函数定义中包含对Auth的引用即可。例如:

angular.module('testSuiteUi2App') 
    .controller('JobCtrl', function ($scope, Auth, socket) { 
    $scope.myVar = 'Hello!'; 
} 

我在代码中遇到了一些麻烦,但这是我想为socketUpdates调用所想要的。

socket.syncUpdates('chat', $scope.chats); 

这告诉系统你想同步一个叫'chat'模型类型的$ scope.chats的数组。你也可以用一个回调做到这一点:

socket.syncUpdates('chat', $scope.chats, function(event, item, object) { 
    $scope.chats = item; // item contains the updated array 
}); 
+0

你怎么想补充一点,听,当有人“GET”创新事物的“POST(保存和更新)”即角fullstack已有功能的自定义监听器? 详细说明,当某人加入时,您将如何向聊天发送消息? – 2015-04-01 15:16:17