Skip to content

Commit

Permalink
Split controllers and services to separate files.
Browse files Browse the repository at this point in the history
  • Loading branch information
vjrantal committed Dec 15, 2014
1 parent ebda73d commit a5d3ce9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 77 deletions.
2 changes: 2 additions & 0 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

<!-- your app's js -->
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
</head>
<body ng-app="chatApp">
<div ng-controller="HeaderController">
Expand Down
77 changes: 0 additions & 77 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,3 @@ chatApp.run(function($ionicPlatform) {
}
});
});

chatApp.controller('HeaderController', function($rootScope, $scope, $ionicPopover, chatService) {
$scope.subheader = chatService.currentChannel();

$ionicPopover.fromTemplateUrl('channel-selector.html', { scope: $scope }).then(function(popover) {
$scope.popover = popover;
});
$scope.openChannelSelector = function($event) {
$scope.loading = true;
$scope.popover.show($event);
chatService.getChannels().then(function(channels) {
$scope.loading = false;
$scope.channels = channels;
});
};

$scope.channelSelected = function($event, channel) {
chatService.setCurrentChannel(channel);
$scope.popover.hide();
};
$rootScope.$on('currentChannelChanged', function (event, channel) {
$scope.subheader = channel.name;
});
});

chatApp.controller('ContentController', function($rootScope, $scope, chatService) {
$scope.messages = chatService.currentChannelMessages();
$rootScope.$on('newMessage', function (event, data) {
$scope.messages = chatService.currentChannelMessages();
});
});

chatApp.controller('FooterController', function($rootScope, $scope, chatService) {
$scope.postMessage = function($event) {
chatService.postCurrentChannel( { text: $scope.message } );
$scope.message = '';
};
});

chatApp.factory('chatService', function($rootScope, $q) {
var chatService = {};

var messages = [ { text: 'Some text' } ];
var currentChannelMessages = function() {
return messages;
};
chatService.currentChannelMessages = currentChannelMessages;

var currentChannel = '(No Channel)';
chatService.currentChannel = function() {
return currentChannel;
};
chatService.setCurrentChannel = function(channel) {
currentChannel = channel;
$rootScope.$broadcast('currentChannelChanged', channel);
};

chatService.postCurrentChannel = function(message) {
messages.push(message);
$rootScope.$broadcast('newMessage', message);
};

chatService.getChannels = function() {
var deferred = $q.defer();
setTimeout(function() {
if (true) {
deferred.resolve([ { name: 'My Channel' }, { name: 'Another' } ]);
} else {
// This would be when unable to fetch channels
deferred.reject([]);
}
}, 1000);
return deferred.promise;
};

return chatService;
});
32 changes: 32 additions & 0 deletions www/js/controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var chatApp = angular.module('chatApp');

chatApp.controller('HeaderController', function($rootScope, $scope, $ionicPopover, chatService) {
$scope.subheader = chatService.currentChannel();

$ionicPopover.fromTemplateUrl('channel-selector.html', { scope: $scope }).then(function(popover) {
$scope.popover = popover;
});
$scope.openChannelSelector = function($event) {
$scope.loading = true;
$scope.popover.show($event);
chatService.getChannels().then(function(channels) {
$scope.loading = false;
$scope.channels = channels;
});
};

$scope.channelSelected = function($event, channel) {
chatService.setCurrentChannel(channel);
$scope.popover.hide();
};
$rootScope.$on('currentChannelChanged', function (event, channel) {
$scope.subheader = channel.name;
});
});

chatApp.controller('ContentController', function($rootScope, $scope, chatService) {
$scope.messages = chatService.currentChannelMessages();
$rootScope.$on('newMessage', function (event, data) {
$scope.messages = chatService.currentChannelMessages();
});
});
48 changes: 48 additions & 0 deletions www/js/services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var chatApp = angular.module('chatApp');

chatApp.controller('FooterController', function($rootScope, $scope, chatService) {
$scope.postMessage = function($event) {
chatService.postCurrentChannel( { text: $scope.message } );
$scope.message = '';
};
});

chatApp.factory('chatService', function($rootScope, $q) {
var chatService = {};

var messages = [ { text: 'Some text' } ];
var currentChannelMessages = function() {
return messages;
};
chatService.currentChannelMessages = currentChannelMessages;

var currentChannel = '(No Channel)';
chatService.currentChannel = function() {
return currentChannel;
};
chatService.setCurrentChannel = function(channel) {
currentChannel = channel;
$rootScope.$broadcast('currentChannelChanged', channel);
};

chatService.postCurrentChannel = function(message) {
messages.push(message);
$rootScope.$broadcast('newMessage', message);
};

chatService.getChannels = function() {
var deferred = $q.defer();
setTimeout(function() {
if (true) {
deferred.resolve([ { name: 'My Channel' }, { name: 'Another' } ]);
} else {
// This would be when unable to fetch channels
deferred.reject([]);
}
}, 1000);
return deferred.promise;
};

return chatService;
});

0 comments on commit a5d3ce9

Please sign in to comment.