2014-09-19 23 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