从数据中返回两个集合中的数据:铁中的函数:路由器流星

时间:2015-12-13 04:31:32

标签: javascript meteor iron-router

不使用publish或waitOn或其他高级技术进行学习。不知怎的,我无法让它发挥作用。

Router.route('/browse/:_id', function(){
            this.render('navigation', {to: 'navbar'});
            this.render('header', {to: 'header'});
            this.render('website', {
              to: 'main',
              data: function(){
                return {
                    websites: Websites.findOne({_id: this.params._id}),
                    comments: Comments.find({siteId: this.params._id})
                   }
              }
            });
        });

此代码适用于“评论”,而“网站”则无法呈现 “网站”下的字段仅在我返回时才会以HTML格式呈现:

data: function(){   /*This works for websites but if used on comments it won't*/
         return Websites.findOne({_id: this.params._id})
       } 

而对于'comments',它只有在我用Object符号或键值对给出时才有效:

data: function(){   /*This works for comments but if used on websites it wont*/
                return {comments: Comments.find({siteId: this.params._id})}
              }

我的问题是我需要在同一个模板中从两个集合中获取数据。任何建议都非常受欢迎。谢谢!

修改也包括我的HTML模板:'网站'模板

<template name="website">
<div class="container">
    /*solved by including #with websites here*/
    <div class="website">
        <div class="website-header js-url">
            <h4>{{title}}</h4>
        </div>
        <div class="website-body">
            <tr><td>URL:</td> <td>{{url}}</td></tr>
            <tr><td>Description</td><td>{{description}}</td></tr>

            <div class="votes">
                <div class="upvote_row">
                    <tr>
                        <td>Up Votes:</td>
                        <td>{{upVote.length}}<a href="#" class="btn btn-default js-upvote"><span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span></a></td>
                    </tr>
                </div>
                <div class="downvote_row">
                    <tr>
                        <td>Down Votes:</td>
                        <td>{{downVote.length}}<a href="#" class="btn btn-default js-downvote"><span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span></a></td>
                    </tr>
                </div>
            </div>

            <tr><td>Posted By:</td><td><a href class="">{{createdBy}}</a> on {{createdOn.toDateString}} at {{createdOn.toLocaleTimeString}} </td></tr>              
        </div>
    </div>
    /* /with */
    <div class="comments">
        <h5>Comment Section</h5>
        {{#if currentUser}}
        <a class="btn btn-default js-toggle-comment-form" href>
            <span class="glyphicon glyphicon-comment" aria-hidden="true"></span>
        </a>
        {{/if}} 
        <div id="comment_form" class="hidden_div">
            <form class="js-save-comment-form">
              <div class="form-group">
                <label for="user_comment">Add Comment</label>
                <input type="textfield" class="form-control" id="user_comment" placeholder="Your views on this website" required>
              </div>

              <button type="submit" class="btn btn-default">Submit</button>
              <button class="btn btn-default js-cancel-submit">Cancel</button>
            </form>
        </div>

        <ol>
        {{#each comments}}
        {{>comment_item}}
        {{/each}}
        </ol>
    </div>

 </div>
</template>

然后我的评论模板完美无瑕。

<template name="comment_item">
    <li class="card-panel teal darken-1 comments">

        <blockquote>
            <p class='card-content'>
                {{userComment}}
            </p>
            <footer>
                <a href="{{url}}">{{postedBy}}</a> on {{postedOn.toDateString}} at {{postedOn.toLocaleTimeString}}
                <a href="#" class="btn btn-default js-upvote">
                    <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
                    <span class="badge badge-notify">{{upVote.length}}</span>
                </a>
                <a href="#" class="btn btn-default js-downvote">
                    <span class="glyphicon glyphicon-arrow-down" aria-hidden="true"></span>
                    <span class="badge badge-notify">{{downVote.length}}</span>
                </a>
             </footer>
        </blockquote>
    </li>
</template>

1 个答案:

答案 0 :(得分:1)

您还应该显示HTML模板代码。我怀疑你直接引用网站对象的密钥而不是{websites: an object, comments: a cursor}。根据您的数据功能,您的数据上下文将为{{keyname}}如果您尝试从您的网站访问keyname,您将无法获得任何结果,因为您的数据上下文没有顶级密钥叫{{1}}。