2015-11-02 91 views
0

我開始爲一個項目使用meteor,並且有幾個概念對於來自angular + php的相當困難。避免在流星模板助手中重複查詢

我想處理兩個助手,用於顯示錶中的記錄列表,基於我存儲在會話變量中的日期範圍。然後,我的模板看起來像這樣,我使用一個名爲noRecords的助手,另一個名爲記錄,記錄存儲實際的文檔集,並在noRecords我試圖存儲爲布爾值,無論記錄文檔集是否爲空。

div(class='col-md-8 col-md-offset-2') 
     if noRecords 
      p There are no records for the selected date 
     else 
      table(class='table table-bordered') 
       thead 
        tr 
         td Id 
         td Name 

       tbody 
        each records 
         +record 

不幸的是我沒有能夠設置在同一時間記錄,並noRecords不重複的查詢,在我的JavaScript代碼的助手現在被定義是這樣的:

records : function(){ 
    var startDate = Session.get('searchDate').setHours(0,0,0,0); 
    var endDate = Session.get('searchDate').setHours(23,59,59,999); 
    var matches = Records.find(
        {date : { 
           $gte : new Date(startDate), 
           $lte : new Date(endDate) 
          } 
        }); 
    return records; 
}, 

noRecords : function(){ 
    var startDate = Session.get('searchDate').setHours(0,0,0,0); 
    var endDate = Session.get('searchDate').setHours(23,59,59,999); 
    var matches = Records.find(
        {date : { 
           $gte : new Date(startDate), 
           $lte : new Date(endDate) 
          } 
        }); 
    return records.count() === 0; 
} 

日期會議變量由事件設置。

我想必須有更好的方式來做到這一點,而不是執行查詢兩次,我試過使用無功變量,但我沒有運氣,因爲我無法讓無功變量更新時minimongo查詢執行。

有沒有辦法實現這個沒有運行兩個查詢?

+0

呃,'函數的GetValues(){退貨/ *你想要的任何值的數組* /}' ?要求「最乾淨的方式」是偏離主題,因爲它是基於意見的(儘管您可能想要重述),但減少代碼重複的通常解決方案是將其包裝在一個函數中。 –

+0

感謝您的回答,我編輯了我的問題,但我擔心的更多的是執行查詢兩次,而不是兩次寫入,將它封裝在函數中仍然會執行兩次。對不起,如果它不是很清楚。 – namelivia

+1

您可以使用'with'來獲取記錄,如果它們的計數是肯定的,則重複。否則,顯示'no records'段落。我已經回答了類似(儘管更簡單)的問題[這裏](http://stackoverflow.com/a/33464551/268093)。 – MasterAM

回答

2

如果你是一個解決重複着同樣的邏輯在多個助手對他們降低到一個助手,並從它返回一個對象,如:

records : function(){ 
    var startDate = Session.get('searchDate').setHours(0,0,0,0); 
    var endDate = Session.get('searchDate').setHours(23,59,59,999); 
    var cursor = Records.find(
        {date : { 
           $gte : new Date(startDate), 
           $lte : new Date(endDate) 
          } 
        }); 
    return { 
     cursor: cursor, 
     empty: cursor.count() === 0, 
     one: cursor.count() === 1 
    } 
} 

而且在你的模板:

if records.one 
    p Found One! 

愚蠢的例子,但它可以更廣泛地使用。

注意,在你的榜樣,你就不會真正需要noRecords幫手,因爲你的模板可以檢查一個空指針:

each records 
    p date 
else 
    p No records! 
+0

返回對象是一個不錯的技巧!謝謝 – namelivia

+0

如果有記錄,最後一個代碼示例將調用'records'助手兩次。你可能意味着別的東西。 – MasterAM

+0

啊這是正確的,你甚至不需要if-each-else,你只需要每個其他人。我會編輯以改進答案(兩者都會起作用) –

0

如果您正在使用火焰,你可以用你的records助手使用{{#each records}}...{{else}}...{{/each}}只有這樣。

<template name="myTemplate"> 
    <div class='col-md-8 col-md-offset-2'> 

    {{#each records}}{{> showRecord}} 

    {{else}}<p>There are no records for the selected date</p> 

    {{/each}} 

    </div> 
</template> 

你也可以使用{{#if records}}...{{else}}...{{/if}}因爲each將遍歷每個項目在records光標

<template name="myTemplate"> 
    <div class='col-md-8 col-md-offset-2'> 

    {{#if records}} 

    <table class='table table-bordered'> 

    <thead> 
     <tr> 
      <td>Id</td> 
      <td>Name</td> 
     </tr> 
     </thead> 

     <tbody> 
     {{#each}}{{> showRecord}}{{/each}} 
     </tbody> 

     </table> 

    {{else}}<p>There are no records for the selected date</p> 

    {{/with}} 

    </div> 
</template> 
+0

據我所知,'#with'不會迭代它的輸入。而且,你的第二個代碼示例有幾個問題。你能指出一些表明'#with'執行迭代的東西嗎? – MasterAM

+0

這是一個錯誤,我的意思是'#each' –