为什么我的Meteor模板在删除自动发布和手动发布/订阅集合时会停止渲染?

时间:2012-07-18 09:42:04

标签: meteor

由于我关闭了自动发布,因为在现实世界中我的模板应该是不切实际的,我的模板停止渲染集合(#each似乎无处循环)。 我已经为集合设置了手动发布/订阅,当我将其登录到控制台时,我可以看到本地集合中有项目,但模板无法呈现项目。

为了保持模板的自动更新性质,手动子/发布集合时我还需要做些什么吗?

这是我创建的稀释测试用例:

// client

Col = new Meteor.Collection('testcol');

// I have tried wrapping this in autosubscribe as well: 
Meteor.subscribe('testcol', function() {
    return Col.find();
});


Template.hello.items = function() {
    var col = Col.find();
    if (col) {
        console.log("Test items" , col);
        return col.fetch().items;
    }
}

// server

if (Meteor.is_server) {
    Col = new Meteor.Collection('testcol');

    Meteor.publish('testcol', function() {
        return Col.find();
    })
}


// bootstrap: 

Meteor.startup(function () {  
  if (Col.find().count() < 5) {
    for (var i=0; i<5; i++) {
        Col.insert({
            title: 'Test ' + i,
            items: [
                {title: 'item 1', value:true},
                {title: 'item 2', value:false},
                {title: 'item 3', value:true}
            ]
        });
    }
    }
})

// Template


<head>
  <title>test</title>
</head>

<body>
  {{> hello}}
</body>

<template name="hello">
  <h1>Where did the data gone to?</h1>
  Items from the test collection: 
  <UL>
  {{#each items}}
    <LI> ITEM: {{title}} 
  {{/each}}
</UL>
</template>

1 个答案:

答案 0 :(得分:3)

#each的Meteor版本需要CursorArray作为您的代码点items,因为这是您的函数返回undefined的每个文档的子属性所以你需要遍历所有Docs然后项目或findOne({_ id:???})循环它的项目;

因此,以下工作(支持版本1)用于返回集合中的所有文档:

  1. return col.find()
  2. return col.find().fetch()
  3. 此外,您应该只在一个公共脚本中声明一次收集,然后在下面相应的工作代码中发布/发布,如果您有任何问题,请回发。

    html的

      1 <head>
      2     <title>test</title>
      3 </head>
      4 
      5 <body>
      6     {{> hello}}
      7 </body>
      8 
      9 <template name="hello">
     10 <h1>Where did the data gone to?</h1> 11 Items from the test collection: 
     12 <ul id="docs">
     13     {{#each docs}}
     14     <li>Doc: {{title}}
     15         <ul class="items">
     16             {{#each items}}
     17             <li>Item: {{title}}</li>
     18             {{/each}}
     19         </ul>
     20     </li>
     21     {{/each}}
     22 </ul>   
     23 </template> 
    

    的.js

      1 // client
      2 //
      3 Col = new Meteor.Collection('testcol');
      4 //
      5 // // I have tried wrapping this in autosubscribe as well: 
      6 if(Meteor.is_client){
      7     Meteor.subscribe('testcol');                                                                                                                                                                        
      8 
      9     Template.hello.docs  = function() {
     10          return Col.find();
     11     }
     12 } 
     13 
     14     
     15 // server
     16          
     17 if (Meteor.is_server) {
     18             
     19     Meteor.publish('testcol', function() {
     20         return Col.find();
     21     });
     22 }
     23 
     24 
     25 // bootstrap: 
     26 
     27 Meteor.startup(function () {  
     28     if (Col.find().count() < 5) {
     29         for (var i=0; i<5; i++) {
     30             Col.insert({
     31                 title: 'Test ' + i,
     32                 items: [
     33             {title: 'item 1', value:true},
     34                 {title: 'item 2', value:false},
     35                 {title: 'item 3', value:true}
     36             ]
     37             });
     38         }
     39     }
     40 });