【问题标题】:Meteor template query change, how to change the content from the database?Meteor模板查询更改,如何从数据库中更改内容?
【发布时间】:2014-02-25 17:54:00
【问题描述】:

我正在用流星写博客,我已经进入搜索部分:现在我想根据搜索条件更改模板变量的数据。到目前为止,我不明白如何使将要在模板中呈现的数据重新排序。

这是我现在拥有的: HTML

<template name="posts">
   {{#each post}}
     <div class = "postWrapper">
     <p>{{postTitle}} - <small><em>{{userName}}<em></small></p>
     <p><small>{{postDate}}</small><p>
     <div class="postText">{{safe 'postText'}}</div>
     <br/>
     </div>
   {{/each}}
</template>

Javascript

Template.posts.post = function(){
   return TestPosts.find({}, {sort: {timeStamp: -1}, limit: 100});
}

如何更改 Template.posts.post 查询,以便按时间戳以外的其他内容进行排序。当事件被触发时,我对 Template.posts.post 查询所做的任何更改最终都无效。

  Template.searchForm.events = {
    "click .cancelBtn": function(){
       $("#lightbox-container, #searchForm, #postForm").hide();
    },
    "click #searchBtn": function(){
        var reply = TestPosts.find({}, {sort: {timeStamp: 1}});
            //what do I need here to be able to change the posts returned 
            //to the template?
    }
  }

我尝试使用 meteor.render 但它什么也没做。任何指导都会很棒。

【问题讨论】:

    标签: javascript templates meteor


    【解决方案1】:

    您将希望开始更加“被动”地思考,而不是程序化思考。如果有帮助,反应性思维有点像陈述性思维。只需声明您想看到的内容,然后让 Meteor 弄清楚如何以及何时。

    考虑到这一点,这可能对您有用(使排序顺序成为会话变量):

    Session.setDefault('sortOrder', {'timestamp': -1});
    Template.posts.post = function(){
       return TestPosts.find({}, {sort: Session.get('sortOrder'), limit: 100});
    }
    
    Template.searchForm.events = {
      "click .cancelBtn": function(){
         $("#lightbox-container, #searchForm, #postForm").hide();
      },
      "click #searchBtn": function(){
          Session.set('sortOrder', {'timestamp': 1});
      }
    }
    

    【讨论】:

    • 没想到会这样,变化很大。感谢您的宝贵时间。
    猜你喜欢
    • 2015-02-22
    • 2021-08-20
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2015-01-19
    • 2017-11-29
    • 2015-01-14
    • 1970-01-01
    相关资源
    最近更新 更多