Currently, I am working on assigning tags during post creation. For this purpose, I have set up a Post model with the following structure:
var mongoose = require('mongoose');
var PostsSchema = {
title: String,
content: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users'
},
comments: [{
text: String,
postedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users'
},
}],
tags: [String]
};
I am in the process of associating checkboxes to the 'tags' array attribute within the Post.
This is how my post router implementation looks like:
///* Create post */
postRouter.route('/').post(function (req, res) {
mongoose.createConnection('localhost', 'CMS');
console.log(req.body);
var post = {
title: req.body.title,
content: req.body.content,
tags: req.body.tags
};
if (typeof req.body.title === "undefined" || typeof req.body.content === "undefined")
{
res.json({message:"Error"});
}else
{
var newPost = new Posts(post);
newPost.save(function (err, post) {
if (err) res.json({message:"Error"});
res.json(post);
});
}
});
The controller associated with this functionality appears as follows:
$scope.createPost = function(post){
postService.createPost(post);
postService.getPosts()
.then(modelPosts);
}
As for the view template, it is configured as below:
div(ng-controller='postController')
h2 Create Post
form
div.form-group
label(for='title') Title
input(type='text', class='form-control', id='title', name='title', placeholder='Title', ng-model='newpost.title', autofocus)
div.form-group
label(for='password') Content
input(type='text', class='form-control', id='content', name='content', placeholder='content', ng-model='newpost.content')
div(ng-controller='tagController')
h2 Tags
div( ng-model='Tags', ng-init='getTags()')
ul( ng-repeat='tag in Tags')
li
label
input(ng-model='newpost.tag',value='{{tag.name}}', type='checkbox', name='tag[]')
span {{tag.name}}
button( ng-click='createPost(newpost)', class='btn btn-small btn-primary') Create Post
Despite successfully rendering tags and creating checkboxes in the view, there seems to be an issue with the binding. When selecting one checkbox, all checkboxes are getting checked simultaneously.