为什么在下面的基本例子里面返回的collection里面呈现的函数是空的?
启用自动发布功能。页面加载调用命令
Coll.find().fetch()
JavaScript控制台返回内部之后正确的设置项流星template.rendered - 为什么集合是空的?
这里是代码
t.js
Coll = new Meteor.Collection("coll");
if (Meteor.isClient) {
Template.tpl.rendered = function(){
console.log(Coll.find().fetch()); // <-- This line prints empty array
};
}
if (Meteor.isServer) {
Meteor.startup(function() {
if (Coll.find().count() === 0) {
var f = ["foo","bar"];
for (var i = 0; i < f.length; i++)
Coll.insert({f: f[i]});
}
});
}
而且t.html
文件
<head>
<title>test</title>
</head>
<body>
{{> tpl}}
</body>
<template name="tpl">
Test tpl
</template>
这是因为您的收藏尚未加载。 'Template.rendered'被触发,并不意味着你的集合被加载。检查[this](http://stackoverflow.com/questions/15129827/)线程。 – 2013-05-01 08:09:28