Created
February 20, 2015 14:07
-
-
Save vitaliy-bobrov/853d18afb1be5f2453d7 to your computer and use it in GitHub Desktop.
Tutorial App part 6 - CommentsCtrl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.controller('CommentsCtrl', ['$scope', '$rootScope', '$stateParams', '$ionicLoading', '$ionicScrollDelegate', 'Comments', function($scope, $rootScope, $stateParams, $ionicLoading, $ionicScrollDelegate, Comments) { | |
Comments.check($stateParams.articleId, $rootScope.token).then(function(data) { | |
$scope.comments_count = data; | |
$scope.presentComments = (parseInt($scope.comments_count) > 0) ? true : false; | |
$scope.showComments = $scope.presentComments; | |
}, function(error) { | |
console.log(error); | |
}); | |
$scope.comments = []; | |
$scope.commentsListVisibility = false; | |
$scope.commentData = {}; | |
$scope.showNewCommentForm = false; | |
$scope.showCommentForm = function() { | |
$scope.showNewCommentForm = true; | |
$ionicScrollDelegate.resize(); | |
}; | |
$scope.showCommentsSwitcher = function() { | |
$scope.showComments = false; | |
$scope.commentsListVisibility = true; | |
$ionicScrollDelegate.resize(); | |
}; | |
$scope.hideComments = function() { | |
$scope.showComments = true; | |
$scope.commentsListVisibility = false; | |
$ionicScrollDelegate.resize(); | |
$ionicScrollDelegate.scrollBottom(true); | |
}; | |
$scope.getComments = function() { | |
if($scope.comments.length === 0) { | |
$ionicLoading.show(); | |
Comments.get($stateParams.articleId).then(function(data) { | |
$scope.comments = data; | |
$scope.showCommentsSwitcher(); | |
$ionicLoading.hide(); | |
}, function(error) { | |
$ionicLoading.hide(); | |
console.log(error); | |
}); | |
} | |
else { | |
$scope.showCommentsSwitcher(); | |
$ionicLoading.hide(); | |
} | |
}; | |
$scope.postComment = function() { | |
$ionicLoading.show(); | |
$scope.commentData.uid = $rootScope.userData.uid; | |
$scope.commentData.nid = $stateParams.articleId; | |
Comments.post($scope.commentData, $rootScope.token).then(function(data) { | |
$scope.comments_count++; | |
if($scope.comments.length === 0) { | |
$scope.getComments(); | |
} | |
else { | |
$scope.comments.unshift({ | |
picture: $rootScope.userData.picture.url, | |
name: $rootScope.userData.name, | |
subject: $scope.commentData.subject, | |
comment_body: $scope.commentData.comment_body.und[0].value | |
}); | |
$scope.showCommentsSwitcher(); | |
$ionicLoading.hide(); | |
} | |
$scope.commentData = {}; | |
$scope.showNewCommentForm = false; | |
}, function(error) { | |
$ionicLoading.hide(); | |
console.log(error); | |
}); | |
}; | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment