Advertisement
  1. Code
  2. JavaScript

Get Started Building Your Blog With Parse.js: Comments

Scroll to top
Read Time: 4 min
This post is part of a series called Building Your Blog with Parse.js.
Get Started Building Your Blog With Parse.js: Delete, Logout, and Single Blog View
Get Started Building Your Blog With Parse.js: Categories
Final product imageFinal product imageFinal product image
What You'll Be Creating

This tutorial series is coming to an end. If you’ve followed along until now, hopefully you have a solid understanding of the MVC framework, and how Parse.js works. Last session, we implemented delete, logout, and single blog view. And in this session, we are going to add the commenting area to the single page view.

The comment section is going to have two parts: a comment form on the top, and a list of comments to that blog post at the bottom:

outcomeoutcomeoutcome

Because you now have built so many features, I am going to speed up a little bit, and group the content into three big steps:

  1. Create the comment model.
  2. Update the BlogView
  3. Add the comment form.

1. Create the Comment Model

Step 1: Comment Class

The model always comes first. Let’s go ahead and create a new class on Parse.com.

This new comment class should have these fields:

  • Pointer blog
  • String authorName
  • String email
  • String content

The pointer points back to the blog post it belongs to, and the other three fields store key information for that comment. For the sake of this tutorial, I’m not going to have the user sign up and log in here, but you can read the Parse.js documentation on the User class for more details.

Step 2: Comment Object

Next, let’s create the Comment model in our blog.js file. You will notice that it’s very similar to the Blog model we created. The only difference is that we are not allowing users to edit their comments later, so we are using an .add() function instead of the .update() function. Again, feel free to make it editable if you want.

1
2
Comment = Parse.Object.extend('Comment', {
3
})

2. Update the BlogView

Moving on to the view, since we have this comment section in BlogView, we can render the comment in the BlogView as well. We will just put that in the .render() function.

Step 1: Fetch Comments

First we need to fetch the comments of the current blog:

1
	
2
render: function() { 
3
	var self = this,
4
		attributes = this.model.toJSON(),
5
		// A new query to filter out all the comment in this blog
6
		query = new Parse.Query(Comment).equalTo("blog", this.model).descending('createdAt'),
7
		// Create a collection base on that new query
8
		collection = query.collection();
9
	// Fetch the collection
10
	collection.fetch().then(function(comments) {
11
		// Store the comments as a JSON object and add it into attributes object
12
		attributes.comment = comments.toJSON();
13
		self.$el.html(self.template(attributes));
14
	});
15
}

Notice that we add .descending('createdAt') to the query so we always display the latest comment at the top.

If we log attributes right before we render it, we can see the object would be structured like this:

1
2
{
3
	ACL: Object,
4
	author: Object,
5
	authorName: "moyi",
6
	comment: Array[2],
7
	...
8
	__proto__: Object
9
}

And you can see that comments are stored as an array, and ready to be rendered.

Step 2: Display Comments

Now we have comments passed into our HTML template within attribute, we can now loop through the array with handlebar.js syntax:

1
2
3
{{#if comment}}
4
<h2>Comments</h2>
5
6
<ul class="blog-comments list-unstyled">
7
	{{#each comment}}
8
	<li class="blog-comment">
9
		<div>
10
<a href="mailto:%7B%7Bemail%7D%7D">{{authorName}}</a> said:</div>
11
		<div>{{content}}</div>
12
	</li>
13
	{{/each}}
14
</ul>
15
{{/if}}
16

If you want to give it a test first, make a few fake comments on Parse.com in your database and refresh this page:

Test CommentTest CommentTest Comment

3. Add the Comment Form

Then, we can move on to adding the comment form.

Step 1: Add HTML

Add this HTML structure right above the comments section:

1
2
<!-- Comment Form -->
3
<h2>Leave a Comment</h2>
4
5
<form class="form-comment" role="form">
6
	<div class="form-group">
7
		<label for="comment-author-name">Name</label>
8
		<input name="authorName" type="text" class="form-control" id="comment-author-name" placeholder="Your name">
9
	</div>
10
	<div class="form-group">
11
		<label for="comment-email">Email address</label>
12
		<input name="email" type="email" class="form-control" id="comment-email" placeholder="Your email">
13
	</div>
14
	<div class="form-group">
15
		<label for="comment-content">Comment</label>
16
		<textarea name="content" class="form-control" rows="3" id="comment-content"></textarea>
17
	</div>
18
	<button type="submit" class="btn btn-default">Submit</button>
19
</form>
20

Step 2: Add an Event for Form Submission

Now let’s add an event in BlogView:

1
2
events: {
3
		'submit .form-comment': 'submit'
4
}

Step 3: Build Out the submit() Function

And then build out the submit() function. If you still remember how we wrote the submit function for WriteBlogView, this is pretty similar.

Just notice this time we need to pass in this.model as blog for the new comment:

1
2
submit: function(e) {
3
	e.preventDefault();
4
	var data = $(e.target).serializeArray(),
5
		comment = new Comment();
6
	comment.add({
7
		blog: this.model,
8
		authorName: data[0].value, 
9
		email: data[1].value,
10
		content: data[2].value
11
	});
12
}

Give it a test, and surely the new comment is now displaying at the top:

New CommentNew CommentNew Comment

Conclusion

This session, you built out the comment function in three quick steps. Hopefully it’s easy for you now, and you can write most of the steps by yourself.

Next session, I’m going to show you one last feature: adding categories to the blog. After that, I will leave you there, as capable Parse.js developers. Stay tuned and leave me all the questions and feedback you may have.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.