2014-04-01 62 views
0

我試圖做一個類別系統,我似乎無法弄清楚如何使其工作。 這裏有一個模擬JS和HTML來證明什麼即時試圖完成結合2集合在1模板Meteor.js

test.js

Categories = new Meteor.Collection('categories'); 
Rooms = new Meteor.Collection('rooms'); 

if (Meteor.isClient) {  
    Template.Category_System.categories = function() { 
     return Categories.find({}); 
    }; 
    Template.Category_System.rooms = function() { 
     return Rooms.find({}); //here i want to return the rooms and users in the Rooms collection but only the rooms that fall under the category and subcategory of the Categories collection 
    }; 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
     Categories.insert({category:"category1",subcategories:[{subcategory:"subcategory1",rooms:[]},{subcategory:"subcategory2",rooms:[]}]}; 
     Categories.insert({category:"category2",subcategories:[{subcategory:"subcategory1",rooms:[]},{subcategory:"subcategory2",rooms:[]}]}; 

     Rooms.insert({category:"category1",subcategory:"subcategory1",room:'Room_01',users:[a,b,c]}); 
     Rooms.insert({category:"category1",subcategory:"subcategory1",room:'Room_02',users:[d,e,f,g,h]}); 
     Rooms.insert({category:"category1",subcategory:"subcategory2",room:'Room_03',users:[i]}); 
     Rooms.insert({category:"category2",subcategory:"subcategory1",room:'Room_01',users:[j,k]}); 
     Rooms.insert({category:"category2",subcategory:"subcategory2",room:'Room_02',users:[l,m,n]}); 
     Rooms.insert({category:"category2",subcategory:"subcategory2",room:'Room_03',users:[o,p,q,r]});    
    }); 
} 

的test.html - >只是模板

<template name="Category_System"> 
    {{#each categories}} 
     {{category}} 
     {{#each subcategories}} 
      {{subcategory}} 
      {{#each rooms}} 
        {{room}}{{users}} 
      {{/each}} 
     {{/each}} 
    {{/each}} 
</template> 

結果我試圖實現

category1 
    -subcategory1 
     -Room_01 a,b,c 
     -Room_02 d,e,f,g,h 
    -subcategory2 
     -Room_03 i 
category2 
    -subcategory1 
     -Room_01 j,k 
    -subcategory2 
     -Room_02 l,m,n 
     -Room_03 o,p,q,r 

在此先感謝

回答

0

根據您的模擬代碼,你可以這樣做以下:

category.js

Categories = new Meteor.Collection('categories'); 
Rooms = new Meteor.Collection('rooms'); 

if (Meteor.isClient) {  
    Template.Category_System.categories = function() { 
     return Categories.find(); 
    }; 
    Template.Category_System.rooms = function (cat) { 
     var _subcat = this.subcategory, 
      _cat = cat; 
     return Rooms.find({ category: _cat, subcategory: _subcat }); 
    }; 
} 

if (Meteor.isServer) { 
    Meteor.startup(function() { 
     if (!Categories.find().count()) { 
      Categories.insert({category:"category1",subcategories:[{subcategory:"subcategory1",rooms:[]},{subcategory:"subcategory2",rooms:[]}]}); 
      Categories.insert({category:"category2",subcategories:[{subcategory:"subcategory1",rooms:[]},{subcategory:"subcategory2",rooms:[]}]}); 
     } 
     if (!Rooms.find().count()) { 
      Rooms.insert({category:"category1",subcategory:"subcategory1",room:'Room_01',users:["a","b","c"]}); 
      Rooms.insert({category:"category1",subcategory:"subcategory1",room:'Room_02',users:["d","e","f","g","h"]}); 
      Rooms.insert({category:"category1",subcategory:"subcategory2",room:'Room_03',users:["i"]}); 
      Rooms.insert({category:"category2",subcategory:"subcategory1",room:'Room_01',users:["j","k"]}); 
      Rooms.insert({category:"category2",subcategory:"subcategory2",room:'Room_02',users:["l","m","n"]}); 
      Rooms.insert({category:"category2",subcategory:"subcategory2",room:'Room_03',users:["o","p","q","r"]}); 
     } 
    }); 
} 

category.html

<head> 
    <title>Category System Test</title> 
</head> 

<body> 
    {{> Category_System}} 
</body> 

<template name="Category_System"> 
    <ul> 
     {{#each categories}} 
     <li> 
      {{category}} 
      <ul> 
       {{#each subcategories}} 
        <li> 
         {{subcategory}} 
         <ul> 
          {{#each rooms ../category }} 
           <li>{{room}} - {{users}}</li> 
          {{/each}} 
         </ul> 
        </li> 
       {{/each}} 
      </ul> 
     </li> 
     {{/each}} 
    </ul> 
</template> 

要注意的關鍵問題是../categoryrooms的幫助程序中的傳遞以及的模板幫助程序,它接受類別參數,並使用當前的subcategory數據上下文過濾Room集合。

如果你運行這個流星0.8.0下,你應該看到下面的輸出... sample output from code found above

您可以通過在每個Category文檔的完成參考rooms陣列使它更容易些。通過用戶界面執行此操作,或者捕獲每個Room文檔的insertId,並在種子時更新相應的Category文檔。

順便說一句,當你播種你的收藏時,你可能會想要拋出一張支票(參見上面的代碼)......否則,每當你做出來的時候你都會收到很多很多的記錄更改並重新啓動應用。

+0

對不起,這麼晚回來,這完美的作品。虐待看完成引用,是的,我忘了添加支票感謝指出。再次感謝所有幫助和快速回復 – blaknt