2017-05-26 68 views
0

顯示「發佈 - 複合集」的孩子考慮在https://github.com/englue/meteor-publish-composite如何在模板視圖

如何顯示在模板視圖嵌套的孩子給出的例子。我的意思是,顯示關於帖子前2條評論關於查看。

我在互聯網上搜索了很多這個模板視圖上的這個兒童樹顯示,沒有找到任何。

代碼

publishComposite('topTenPosts', { 
    find() { 
     // Find top ten highest scoring posts 
     return Posts.find({}, { sort: { score: -1 }, limit: 10 }); 
    }, 
    children: [ 
     { 
      find(post) { 
       // Find post author. Even though we only want to return 
       // one record here, we use "find" instead of "findOne" 
       // since this function should return a cursor. 
       return Meteor.users.find(
        { _id: post.authorId }, 
        { fields: { profile: 1 } }); 
      } 
     }, 
     { 
      find(post) { 
       // Find top two comments on post 
       return Comments.find(
        { postId: post._id }, 
        { sort: { score: -1 }, limit: 2 }); 
      }, 
      children: [ 
       { 
        find(comment, post) { 
         // Find user that authored comment. 
         return Meteor.users.find(
          { _id: comment.authorId }, 
          { fields: { profile: 1 } }); 
        } 
       } 
      ] 
     } 
    ] 
}); 
+1

客戶端數據庫中沒有實際的樹。每個文檔都轉到相應的集合。到目前爲止你嘗試了什麼,你卡在哪裏? – MasterAM

回答

0

使用火焰它應該只是一組簡單的,你搜索在助手的相關評論和作者模板,顯示了文章和評論嵌套{{#each}}循環。

HTML:

<template name="posts"> 
{{#each posts}} 
    Title: {{title}} posted on: {{date}} by: {{authorName}} 
    {{#each comments}} 
    {{> comment}} 
    {{/each}} 
{{/each}} 
</template> 

<template name="comment"> 
Post comment {{body}} by {{authorName}} on {{createdAt}} 
</template> 

現在的助手:

Template.posts.helpers({ 
    posts(){ 
    return Posts.find({}, { sort: { score: -1 }, limit: 10 }); 
    }, 
    comments(){ 
    return Comments.find({ postId: this._id },{ sort: { score: -1 }, limit: 2 }); 
    }, 
    authorName(){ 
    return Meteor.users.findOne(this.authorId).username; 
}); 

Template.comment.helpers({ 
    authorName(){ 
    return Meteor.users.findOne(this.authorId).username; 
    }, 
}); 

注意這些助手使用thisthis將是在評估點處的數據上下文的值。在{{#each}}this內部具有當前文檔的值,即具有鍵的對象。

如果您願意,您可以通過創建一個全球幫助來保持authorName DRY幫助。

+0

你好嗎?如果我們在模板中立即創建訂閱和幫助器,那麼'publish-composite'包需要什麼?讓嵌套的孩子?我還沒有理解這背後的目的。我的代碼仍然可以在沒有'publish-composite'包的情況下運行,正確的方法是使用上面的代碼? –

+1

發佈複合可以幫助您確保您準確發佈所需內容,而不再需要更多內容。有幾種方法可以在這裏發佈嵌套數據。 –