Skip to content

Commit

Permalink
refactored/reordered/commented controller for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
btford committed Jul 16, 2012
1 parent 7231301 commit 999ea7d
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions public/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

function AppCtrl($scope, socket) {

// Socket listeners
// ================

socket.on('set:name', function (data) {
$scope.name = data.name;
$scope.users = data.users;
Expand All @@ -17,29 +20,6 @@ function AppCtrl($scope, socket) {
$scope.$apply();
});

var changeName = function (oldName, newName) {

// retroactively rename that user's messages
$scope.messages.forEach(function (message) {
if (message.user === oldName) {
message.user = newName;
}
});

// rename user in list of users
var i;
for (i = 0; i < $scope.users.length; i++) {
if ($scope.users[i] === oldName) {
$scope.users[i] = newName;
}
}

$scope.messages.push({
user: 'chatroom',
text: 'User ' + oldName + ' is now known as ' + newName + '.'
});
}

socket.on('change:name', function (data) {
changeName(data.oldName, data.newName);
$scope.$apply();
Expand Down Expand Up @@ -72,6 +52,35 @@ function AppCtrl($scope, socket) {
$scope.$apply();
});

// Private helpers
// ===============

var changeName = function (oldName, newName) {

// retroactively rename that user's messages
$scope.messages.forEach(function (message) {
if (message.user === oldName) {
message.user = newName;
}
});

// rename user in list of users
var i;
for (i = 0; i < $scope.users.length; i++) {
if ($scope.users[i] === oldName) {
$scope.users[i] = newName;
}
}

$scope.messages.push({
user: 'chatroom',
text: 'User ' + oldName + ' is now known as ' + newName + '.'
});
}

// Methods published to the scope
// ==============================

$scope.changeName = function () {
socket.emit('change:name', {
name: $scope.newName
Expand Down

0 comments on commit 999ea7d

Please sign in to comment.