Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example blog with posts and comments #6

Open
loongmxbt opened this issue Apr 7, 2015 · 5 comments
Open

Example blog with posts and comments #6

loongmxbt opened this issue Apr 7, 2015 · 5 comments
Labels

Comments

@loongmxbt
Copy link

Could you please give a detailed example blog having comments related to a post?

The comments_list and comment_create templates are at front-end in post page, and I can also see the comments I submitted at admin panel -> comments entity -> index page.

Another problem is that for different entities, such as posts and resources, I want to have a unified comments functionality related to them, how can I do that? And can you give an example for that? Thanks a lot!

@nicolaslopezj
Copy link
Member

Not all collections must be entities, entities should only be the ones that are editable in the admin.
So, lets start:

Declare the collection

Comments = new Mongo.Collection('comments');
Comments.attachSchema(new SimpleSchema({
    content: {
        type: String,
        autoform: {
            label: false,
            placeholder: 'Message'
        }
    },
    postId: {
        type: String,
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date;
        }
    },
    createdBy: {
        type: String,
        autoValue: function() {
            return this.userId;
        }
    }
}));

Create the publication

Meteor.publish('commentsForPost', function (postId) {
    return Comments.find({ postId: postId });
});

Use template level subscriptions to subscribe to the comments

Template.post.onRendered(function() {
    this.subscribe('commentsForPost', this.data._id)
})

Create the template helper to fetch the comments

Template.post.helpers({
    comments: function () {
        return Comments.find({ postId: this._id }, { sort: { createdAt: -1 } });
    }
});

Put them in the html code

<template name="post">
    ...
    {{# each comments }}
        <p><b>{{ getUserName }}</b></p>
        <p>{{ content }}</p>
        <p>{{ timeago createdAt }}</p>
    {{/ each }}
</template>

Create the form and with it event

Template.post.events({
    'click .comment-btn': function () {
        Comments.insert({ postId: this._id, content: $(".comment-content").val() });
    }
});

If you have any questions you can ask

@loongmxbt
Copy link
Author

The remind "Not all collections must be entities, entities should only be the ones that are editable in the admin." really helps a lot!

@loongmxbt
Copy link
Author

Another question maybe not directly related to orion, that is how to define dynamic Schema (form)?

For example, in my resource_create template, I want to do the following:
If I choose resource type movie, I want to have fields like IMDB_link, director, characters and so on.
If I choose resource type series, I want to have fields like season, number and so on.
And use these fields to combine a final resource title.

Which means in MongoDB, I'd like to store resources with different schema (minor difference) in one collection. How could I do that if I assign SimpleSchema?

@nicolaslopezj
Copy link
Member

Thats completely aldeed/meteor-simple-schema. Maybe you can make the dynamic values optional depending if a Boolean "isMovie"

@loongmxbt
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants