在第一个模板渲染时没有渲染数据数组
Meteorjs Data array not rendered at first template rendered
我正在研究Meteorjs中的一个项目,但是我在渲染一些数据时遇到了问题。
问题是:在第一个模板渲染时,没有html。然后我将模板切换到"MAP"视图并返回到我的"LIST"视图,现在有了包含不同训练器的html。
我有一个routeController
,它做了一些额外的操作来从Algolia
中获得一些data
,并将其推入data.trainers
数组。
例如(在trainers_controller.js
中):
data: function() {
var trainers_tab = [];
// algolia
var client = algoliasearch('XXXXXXXXX', 'XXXXXXXXXX');
var index = client.initIndex('myindex');
if(Meteor.user()) {
tab = Trainers.find({});
tab.forEach(function(trainer) {
index.getObject(trainer.doctor_id, function (err, content) {
if (err) {
console.error(err);
return;
}
// geoloc address
// TODO : retrieve training exact address
// push to array
trainers_tab.push({
objectID: content.objectID,
name: content.name,
city: content.city,
speciality: content.speciality,
photo: content.photo,
about: content.about,
email: content.email,
phone: content.phone
//address:
});
});
});
}
var data = {
params: this.params || {},
trainers: trainers_tab
};
return data;
}
我正好有我需要的data.trainers
.
接下来,我尝试用
的基本方法将训练器渲染到我的template
中{{#each trainer in trainers}}
Do some stuff in html yeah !
{{/each}}
我决定并且可以用火焰的{{>TemplateDynamic }}
空格键切换视图
根据您的函数定义的位置,您可以使用ReactiveVar
并将其设置为data
函数中的数据,即。不是返回data
,而是将对象设置为ReactiveVar:
Template.foo.onCreated(function() {
this.myData = new ReactiveVar(null);
});
Template.foo.onRendered(function() {
// Run your data function here
const data = {
params: this.params || {},
trainers: trainers_tab
};
this.myData.set(data);
});
最后,使用帮助器检索数据:
Template.foo.helpers({
data: function() {
return Template.instance().myData.get();
},
});
这样,如果你的模板在数据被检索之前呈现,它将在myData在你的函数中设置后更新并重新呈现。
相关文章:
- 使用数据数组创建多个类似组件
- WebAudio API数据数组大小
- Jquery 循环一次或在数据数组中显示一次数据
- Cakephp 访问不在模型数据数组上的输入
- 使用来自 Ajax 响应的 JSON 数据数组
- Angular JS(离子)数据数组到$scope元素
- handontable:在不更改数据数组/对象的情况下隐藏某些列
- 如何使用for循环语句通过Ajax发送大数据数组
- 压缩 JS 中的数据数组
- 传递的参数D3提示回调是完整的数据数组
- 如何在数据数组的每次迭代中创建一个内容为“i+1”的新 HTML 元素
- 在 laravel 4 上使用循环将数据数组存储到单个数组中
- 为什么它说输入数据数组的格式不正确 jqchart.
- Javascript - 使用变量 RegExp 匹配数据数组中的多个关键字
- 尝试将数据数组设置为从视图调用函数后$scope
- 如何在 PHP 中创建 jqplot 数据数组
- 如何使用该数据数组从左向右移动画布
- 如何根据数字列从 Parse 中对数据数组进行排序
- 为什么 php json 响应不会将数据数组发送回 ajax jquery 请求
- 如何将内容添加到 JQuery Flot 图表数据数组工具提示