diff --git a/modules/chat/client/chat.client.module.js b/modules/chat/client/chat.client.module.js index 80ef9c29..5e7669f8 100644 --- a/modules/chat/client/chat.client.module.js +++ b/modules/chat/client/chat.client.module.js @@ -1,4 +1,6 @@ -'use strict'; +(function (app) { + 'use strict'; -// Use Applicaion configuration module to register a new module -ApplicationConfiguration.registerModule('chat'); + app.registerModule('chat'); + app.registerModule('chat.routes', ['ui.router']); +})(ApplicationConfiguration); diff --git a/modules/chat/client/config/chat.client.config.js b/modules/chat/client/config/chat.client.config.js index 381ef5d6..d0efbebe 100644 --- a/modules/chat/client/config/chat.client.config.js +++ b/modules/chat/client/config/chat.client.config.js @@ -1,12 +1,17 @@ -'use strict'; +(function () { + 'use strict'; -// Configuring the Chat module -angular.module('chat').run(['Menus', - function (Menus) { + angular + .module('chat') + .run(menuConfig); + + menuConfig.$inject = ['Menus']; + + function menuConfig(Menus) { // Set top bar menu items Menus.addMenuItem('topbar', { title: 'Chat', state: 'chat' }); } -]); +})(); diff --git a/modules/chat/client/config/chat.client.routes.js b/modules/chat/client/config/chat.client.routes.js index 38649c68..edfc959e 100644 --- a/modules/chat/client/config/chat.client.routes.js +++ b/modules/chat/client/config/chat.client.routes.js @@ -1,15 +1,22 @@ -'use strict'; +(function () { + 'use strict'; -// Configure the 'chat' module routes -angular.module('chat').config(['$stateProvider', - function ($stateProvider) { + angular + .module('chat.routes') + .config(routeConfig); + + routeConfig.$inject = ['$stateProvider']; + + function routeConfig($stateProvider) { $stateProvider .state('chat', { url: '/chat', templateUrl: 'modules/chat/client/views/chat.client.view.html', + controller: 'ChatController', + controllerAs: 'vm', data: { roles: ['user', 'admin'] } }); } -]); +})(); diff --git a/modules/chat/client/controllers/chat.client.controller.js b/modules/chat/client/controllers/chat.client.controller.js index edb9b272..85c060fe 100644 --- a/modules/chat/client/controllers/chat.client.controller.js +++ b/modules/chat/client/controllers/chat.client.controller.js @@ -1,43 +1,55 @@ -'use strict'; +(function () { + '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 = []; + angular + .module('chat') + .controller('ChatController', ChatController); - // If user is not signed in then redirect back home - if (!Authentication.user) { - $location.path('/'); + ChatController.$inject = ['$scope', '$state', 'Authentication', 'Socket']; + + function ChatController($scope, $state, Authentication, Socket) { + var vm = this; + + vm.messages = []; + vm.messageText = ''; + vm.sendMessage = sendMessage; + + init(); + + function init() { + // If user is not signed in then redirect back home + if (!Authentication.user) { + $state.go('home'); + } + + // Make sure the Socket is connected + if (!Socket.socket) { + Socket.connect(); + } + + // Add an event listener to the 'chatMessage' event + Socket.on('chatMessage', function (message) { + vm.messages.unshift(message); + }); + + // Remove the event listener when the controller instance is destroyed + $scope.$on('$destroy', function () { + Socket.removeListener('chatMessage'); + }); } - // 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 () { + function sendMessage() { // Create a new message object var message = { - text: this.messageText + text: vm.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'); - }); + vm.messageText = ''; + } } -]); +})(); diff --git a/modules/chat/client/views/chat.client.view.html b/modules/chat/client/views/chat.client.view.html index c50632b0..b1ffe841 100644 --- a/modules/chat/client/views/chat.client.view.html +++ b/modules/chat/client/views/chat.client.view.html @@ -1,22 +1,22 @@ -
+
-
+
- + - +