Skip to content

Commit

Permalink
Implemented scope events to load messages after clicking on a user
Browse files Browse the repository at this point in the history
  • Loading branch information
yeusiukou committed Feb 11, 2016
1 parent e31ce1b commit 83e41b4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 35 deletions.
16 changes: 10 additions & 6 deletions src/app/components/chat/chat.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
return directive;

/** @ngInject */
function ChatController(moment) {
function ChatController($scope) {
var chat = this;

chat.messages = [];
Expand All @@ -29,7 +29,11 @@
avatar: faker.image.avatar()
};
chat.sendMessage = sendMessage;
getMessages();

$scope.$on("OPEN_CHAT", function(event, user){
chat.messages = [];
getMessages(user);
});

function sendMessage(){
chat.messages.push({
Expand All @@ -41,12 +45,12 @@
chat.myMessage = "";
}

function getMessages(){
for(var i=0; i<15; i++)
function getMessages(user){
for(var i=0; i<5; i++)
chat.messages.push({
text: faker.lorem.paragraph(),
name: chat.name,
avatar: chat.avatar
name: user.name,
avatar: user.image
});
}

Expand Down
32 changes: 5 additions & 27 deletions src/app/components/main/main.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,14 @@

return directive;

function MainController($timeout, webDevTec, toastr, $mdToast) {
function MainController($scope) {
var vm = this;

vm.awesomeThings = [];
vm.classAnimation = '';
vm.creationDate = 1454691257479;
vm.showToastr = showToastr;
// this.openChat = openChat;

activate();

function activate() {
getWebDevTec();
$timeout(function() {
vm.classAnimation = 'rubberBand';
}, 4000);
}

function showToastr() {
$mdToast.show($mdToast.simple().textContent('Hello!'));
// Could also do $mdToast.showSimple('Hello');
// logger.info("Hello");
}

function getWebDevTec() {
vm.awesomeThings = webDevTec.getTec();

angular.forEach(vm.awesomeThings, function(awesomeThing) {
awesomeThing.rank = Math.random();
});
}
// function openChat(user){
// $scope.$broadcast("OPEN_CHAT", user);
// }
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/app/components/sidebar/sidebar.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@
return directive;

/** @ngInject */
function SidebarController(moment) {
function SidebarController($scope) {
var vm = this;

vm.users = [];
vm.openChat = openChat;
getUsers();

function getUsers(){
for(var i=0; i<15; i++)
vm.users.push({
id: i,
name: faker.name.findName(),
image: faker.image.avatar()
});
}

function openChat(user){
$scope.$emit("OPEN_CHAT", user);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/components/sidebar/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<md-list>
<!-- List item #1 -->
<md-list-item ng-repeat="user in vm.users">
<md-button flex>
<md-button flex ng-click="vm.openChat(user)">
<div layout="row" layout-align="left center">
<img ng-src="{{user.image}}" class="md-avatar"></img>
{{user.name}}
Expand Down

0 comments on commit 83e41b4

Please sign in to comment.