流星:从Iron Router传递到模板的数据,在第一次加载时为空 [英] Meteor: data passed to template from Iron Router, is empty at first load
问题描述
我正面临一个奇怪的问题. 我正在使用Iron Router控制器将数据传递到模板:
I'm facing a strange problem. I'm using Iron Router controller to pass data to template:
Router.route('/wards/add/:_id?', {name: 'wards.add', controller: 'WardAddController'});
WardAddController = RouteController.extend({
action: function() {
this.render('addWard', {
data: function(){
return { hospitals : Hospitals.find({}), hospital_id : this.params._id }
}
});
}
});
我返回一个变量医院",该变量应包含所有收集数据. 模板:
I return a variable 'hospitals', that should contain all the collection data. Template:
<div class="jumbotron">
{{#each hospitals}}
{{name}}<br>
{{/each}}
</div>
在第一次加载页面时,如果我直接键入页面的url,则没有任何项目. 如果我在浏览器控制台中键入Hospitals.find({}).fetch()(不安全处于活动状态),它将返回一个空对象.
At first page load, if I type the directly the url of the page, there are no items. If I type Hospitals.find({}).fetch() (insecure is active) in the browser console, it return an empty object.
但是,如果我更改页面,浏览网站一段时间并返回列表页面,则会显示项目.
But if i change pages, navigating on the website a while, and return the the listing page, items appears.
有什么主意吗?
推荐答案
在服务器文件夹中,添加publish.js并在其中添加:
In the server folder, add publish.js and inside it add:
Meteor.publish('hospitals', function() {
return Hospitals.find({});
});
然后尝试从您的管理员处订阅医院:
Then try subscribing to hospitals from your controller:
WardAddController = RouteController.extend({
action: function() {
this.render('addWard', {
waitOn: function() {
return [
Meteor.subscribe('hospitals')
];
},
data: function(){
return { hospitals : Hospitals.find({}), hospital_id : this.params._id }
}
});
}
});
这篇关于流星:从Iron Router传递到模板的数据,在第一次加载时为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!