2014-02-25 84 views
0

我正在使用流星的博客上工作,我已經進入了搜索部分:現在我想根據搜索條件更改數據到模板變量。到目前爲止,我不知道如何使模板中要呈現的數據重新排序。流星模板查詢更改,如何更改數據庫中的內容?

這是我現在有: 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,但它什麼也沒做。任何指導都會很棒。

回答

1

你會想開始思考更多的「反應」,而不是程序上。如果有幫助,反應性思維有點像聲明式思維。只要聲明你想看到什麼,讓流星知道如何以及何時。

考慮到這一點,這可能爲你工作(使排序順序會話變量):

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}); 
    } 
} 
+0

我從來沒有這樣想過呢,這改變了很多。感謝您的時間。 –