Files
meanTorrent/modules/chat/client/controllers/chat.client.controller.js
mleanos 0fa9b9579a Added missing dependency injection in Chat
During my last PR merge, the dependency injection for Authentication and
$location weren't merged properly. I added them back to the Chat client
controller.
2015-07-25 16:05:48 -07:00

43 lines
1.3 KiB
JavaScript

'use strict';
// Create the 'chat' controller
angular.module('chat').controller('ChatController', ['$scope', '$location', 'Authentication', 'Socket',
function($scope, $location, Authentication, Socket) {
// Create a messages array
$scope.messages = [];
// If user is not signed in then redirect back home
if (!Authentication.user) $location.path('/');
// Make sure the Socket is connected
if (!Socket.socket) {
Socket.connect();
}
// Add an event listener to the 'chatMessage' event
Socket.on('chatMessage', function(message) {
$scope.messages.unshift(message);
});
// Create a controller method for sending messages
$scope.sendMessage = function() {
// Create a new message object
var message = {
text: this.messageText
};
// Emit a 'chatMessage' message event
Socket.emit('chatMessage', message);
// Clear the message text
this.messageText = '';
};
// Remove the event listener when the controller instance is destroyed
$scope.$on('$destroy', function() {
Socket.removeListener('chatMessage');
});
}
]);