Skip to content

Instantly share code, notes, and snippets.

@vitaliy-bobrov
Created February 20, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaliy-bobrov/853d18afb1be5f2453d7 to your computer and use it in GitHub Desktop.
Save vitaliy-bobrov/853d18afb1be5f2453d7 to your computer and use it in GitHub Desktop.
Tutorial App part 6 - CommentsCtrl
.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