Commit 5e8db4bd45b12a5b1a67452418dcb2edbb02b829

Authored by Victor Costa
1 parent 9d63c8c4
Exists in master and in 1 other branch dev-fixes

Emit an event when a comment was created

src/app/article/comment/comments.component.ts
@@ -8,13 +8,17 @@ import { CommentComponent } from "./comment.component"; @@ -8,13 +8,17 @@ import { CommentComponent } from "./comment.component";
8 templateUrl: 'app/article/comment/comments.html', 8 templateUrl: 'app/article/comment/comments.html',
9 directives: [PostCommentComponent, CommentComponent] 9 directives: [PostCommentComponent, CommentComponent]
10 }) 10 })
11 -@Inject(CommentService) 11 +@Inject(CommentService, "$rootScope")
12 export class CommentsComponent { 12 export class CommentsComponent {
13 13
14 - comments: noosfero.Comment[]; 14 + comments: noosfero.Comment[] = [];
15 @Input() article: noosfero.Article; 15 @Input() article: noosfero.Article;
16 16
17 - constructor(private commentService: CommentService) { } 17 + constructor(private commentService: CommentService, private $rootScope: ng.IScope) {
  18 + $rootScope.$on("comment.received", (event: ng.IAngularEvent, comment: noosfero.Comment) => {
  19 + this.comments.push(comment);
  20 + });
  21 + }
18 22
19 ngOnInit() { 23 ngOnInit() {
20 this.commentService.getByArticle(this.article).then((result: noosfero.RestResult<noosfero.Comment[]>) => { 24 this.commentService.getByArticle(this.article).then((result: noosfero.RestResult<noosfero.Comment[]>) => {
src/app/article/comment/post-comment.component.ts
@@ -6,7 +6,7 @@ import { NotificationService } from &quot;../../shared/services/notification.service&quot; @@ -6,7 +6,7 @@ import { NotificationService } from &quot;../../shared/services/notification.service&quot;
6 selector: 'post-comment', 6 selector: 'post-comment',
7 templateUrl: 'app/article/comment/post-comment.html' 7 templateUrl: 'app/article/comment/post-comment.html'
8 }) 8 })
9 -@Inject(CommentService, NotificationService) 9 +@Inject(CommentService, NotificationService, "$rootScope")
10 export class PostCommentComponent { 10 export class PostCommentComponent {
11 11
12 @Input() article: noosfero.Article; 12 @Input() article: noosfero.Article;
@@ -14,13 +14,14 @@ export class PostCommentComponent { @@ -14,13 +14,14 @@ export class PostCommentComponent {
14 14
15 @Input() replyOf: noosfero.Comment; 15 @Input() replyOf: noosfero.Comment;
16 16
17 - constructor(private commentService: CommentService, private notificationService: NotificationService) { } 17 + constructor(private commentService: CommentService, private notificationService: NotificationService, private $rootScope: ng.IScope) { }
18 18
19 save() { 19 save() {
20 if (this.replyOf) { 20 if (this.replyOf) {
21 this.comment.reply_of_id = this.replyOf.id; 21 this.comment.reply_of_id = this.replyOf.id;
22 } 22 }
23 this.commentService.createInArticle(this.article, this.comment).then(() => { 23 this.commentService.createInArticle(this.article, this.comment).then(() => {
  24 + this.$rootScope.$emit("comment.received", this.comment);
24 this.notificationService.success({ title: "Good job!", message: "Comment saved!" }); 25 this.notificationService.success({ title: "Good job!", message: "Comment saved!" });
25 }); 26 });
26 } 27 }