【问题标题】:Grouping by date using Meteor使用 Meteor 按日期分组
【发布时间】:2014-08-16 06:18:49
【问题描述】:

我想使用 Meteor 按日期对帖子进行分组,并且仍然保留其特有的反应性。我不知道这真的可能。

我正在按照“发现流星”一书中的说明开发基于显微镜的网站,但我很难做出小的改变,因为我没有使用流星的经验。

我对原始密码书进行了小幅调整,但没有真正改变其原始结构。

我需要做的是按日期对帖子进行分组,使它们看起来像这样:

  • 今天
    • 发布 7
    • 发布 6
    • 发布 5
  • 昨天
    • 发布 4
    • 发布 3
  • 2014 年 8 月 11 日
    • 发布 2
    • 发布 1

我目前的代码结构如下:

/client/view/posts/posts_list.js

Template.postsList.helpers({    
    posts: function() {
        return Posts.find({}, {sort: {submittedDate: -1}});
    }
});

/client/view/posts/posts_list.html

<template name="postsList">
    <div class="posts">
        {{#each posts}}
            {{> postItem}}  
        {{/each}}
        {{#if nextPath}}
            <a class="load-more" href="{{nextPath}}">Show more</a>
        {{/if}}
    </div>
</template>

/client/view/posts/post_item.js

Template.postItem.helpers({
    ownPost: function () {
        return this.userId == Meteor.userId();
    },
});

/client/view/posts/post_item.html

<template name="postItem">
    <div class="post">
        <div class="post-content">
            <h3><a href="{{url}}">{{title}}</a><span>{{description}}</span></h3>
        </div>
        <div class="post-comments">
            <a href="{{pathFor 'postPage'}}">{{commentsCount}} comments</a>
        </div>
        {{#if ownPost}} 
            <a href="{{pathFor 'postEdit'}}">Edit</a>
        {{/if}}     
    </div>
</template>

/collections/posts.js

Posts = new Meteor.Collection('posts');  
Posts.allow({
    update: ownsDocument,
    remove: ownsDocument
});

Meteor.methods({
    post: function(postAttributes) {
        var user = Meteor.user(), postWithSameLink = Posts.findOne({url: postAttributes.url});
        if(!user)
        throw new Meteor.Error(401, "You need to be a registered user to do this");
        if(!postAttributes.title)
            throw new Meteor.Error(422, "Please, fill the name field");
        if(!postAttributes.description)
            throw new Meteor.Error(422, "Please, fill the description field");
        if(!postAttributes.url)
            throw new Meteor.Error(422, "Please, fill the URL field");
            if(postAttributes.url && postWithSameLink) {
            throw new Meteor.Error(302, "This URL already exist", postWithSameLink._id);
        }

        var post = _.extend(_.pick(postAttributes, 'url', 'title', 'description'), {
            userId: user._id,
            author: user.username,
            submittedDate: new Date().getTime(),
            commentsCount: 0
        });     

        var postId = Posts.insert(post);
        return postId;
    }
});

/server/publications.js

Meteor.publish('posts', function(options) {
    return Posts.find({}, options);
});

Meteor.publish('singlePost', function(id) {
    return id && Posts.find(id);
});

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

Meteor.publish('notifications', function() {
    return Notifications.find({userId: this.userId});
});

我尝试了几个在这里和 GitHub 上找到的解决方案,但我无法让它们中的任何一个工作。我尝试过的解决方案是:

StackOverflow 1(流星问题 644):Are "group by" aggregation queries possible in Meteor, yet?

GitHub Arunoda 的方法:https://github.com/arunoda/meteor-smart-collections/issues/47

另外,我尝试使用 list-grouper(atmosphere 包),但无法在我的代码中实现包的说明。

如果这里有任何好心人知道如何做到这一点,我将不胜感激。非常非常非常! :)

【问题讨论】:

  • 嗨!我编写了 list-grouper 包,可能可以向您展示如何使用它,但我目前不在电脑前。我会回复你的。
  • 好的。谢谢@KristofferK。我会很感激的! :)

标签: javascript meteor


【解决方案1】:

我没有在流星文档对象上找到“submittedDate”属性。当您将新文档(条目)插入数据库时​​,您可能需要将当前日期/时间指定为参数之一,以与其余参数一起使用。

【讨论】:

  • 是的!我正在 /collections/posts.js 文件上执行此操作(我猜),方法是: var post = .extend(.pick(postAttributes, 'url', 'title', ' description'), { userId: user._id, author: user.username, submitDate: new Date().getTime(), cmetsCount: 0
  • 对不起,我错过了。我自己是流星新手,所以我无法为您提供进一步的帮助。我希望你能找到你正在寻找的答案。
【解决方案2】:

我认为,那里的代码比处理您的核心问题所需的代码多。我已经简化了一点——希望这会有所帮助。我只想指出我不是专家——也在学习。我没有时间测试这个。但我认为它解决了一种完成你需要的方法。

我认为模板的基本结构看起来像这样。

<ul>
    {{#each post}}
        {{if isNewDate}}
            <ul>
               <li>{{writeDate}}</li>
            </ul>
        {{/if}}
        <li>{{postName}}</li>
    {{/each}}
</ul>

我认为 helpers.js 文件可能看起来像这样。

var tempDate = {}; //we need a variable outside the scope of the helper functions 
Template.foobar.helpers ({
    post: function(){
        Posts.find({}, {sort: {submittedDate: -1}}); 
    },
    isNewDate: function(){
        //test if the date of this record matches the date of the previous record
        //if it's new, return true.
        //see momentJS -- makes dealing with dates sooooo much easier
        if( moment().format("LL", this.submittedDate) !== moment().format("LL", tempDate)){
            tempDate = this.submittedDate;
            return true;
        } else {
            return false;
        }
    },
    writeDate: function(){
        return moment().format("LL", this.timestamp);
    }
})

【讨论】:

  • 嗨,杰。感谢您提交答案。不幸的是我无法让它工作! :(
  • 我在浏览器控制台上不断收到以下错误:未捕获的类型错误:无法读取未定义的属性“帮助者”。帖子在浏览器中不按日期分组,也不显示日期。我将 moment 包添加到项目中,并将您在 {{if isNewDate}} 中的代码更改为 {{#if isNewDate}} 并将 {{postName}} 更改为 {{postItem}} 但到目前为止它也没有工作。 :(
猜你喜欢
  • 2011-02-19
  • 1970-01-01
  • 2019-09-10
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2021-09-30
  • 2012-05-28
  • 2011-12-30
相关资源
最近更新 更多