Book Image

Node.js By Example

Book Image

Node.js By Example

Overview of this book

Table of Contents (18 chapters)
Node.js By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Liking posts and counting the number of likes


The users of our social network should be able to see a Like button. By clicking on it, they will send a request to the API and our task is to count these clicks. Of course, only one click per user is allowed. As in the previous section, we will start by updating the user interface. Let's add another button next to the Share one in the following way:

// frontend/tpl/home.html
<input type="button" value="Like"  on-click="like:{{posts[index].id}}" />
{{#if !posts[index].ownPost}}
<input type="button" value="Share"  on-click="share:{{posts[index].id}}" />
{{/if}}

The new button dispatches a like event, and we will again pass the ID of the post. It is actually similar to the share event. Also, the liking action will use the same type of communication with the backend. So, it makes sense to refactor our code and use only one function for both the features. In the previous section, we added the sharePost method to the models/Content.js file...