【发布时间】:2016-05-02 18:46:08
【问题描述】:
模板渲染不工作
当用户成功登录到系统时,我重定向到配置文件页面,该时间数据未获取,但如果我访问另一个页面并返回配置文件页面,该时间它工作正常。同样,当我重新加载页面时,它也无法正常工作
这里是代码
Template.profile.rendered = function(){
var user_email = {};
user_email.mail = Session.get('email');
var imgName = Session.get('image');
Meteor.call("imgSend",imgName,function(error, result){
$('.user_profile_image').attr("src",result)
});
Meteor.call("getLinkMeta",user_email,function(error, result){
var link_all_info = [];
var walldata = [];
var total = result.length;
var processed = 0;
var t = result.forEach(function (entry){
var link_info = {};
link_info.link_id = entry._id;
Meteor.call("getCommentList",link_info, function (error, res){
if(error){
console.log("e");
}else{
entry.comments = res;
}
processed++
if(processed == total){
//walldata=result;
}
});
});
Template.profile.walldata = function(){
return result;
};
//return result;
});
}
Router.route('profile', {
path: '/profile',
data: function() {
/* Meteor.subscribe("Users");
Meteor.subscribe("Link");
Meteor.subscribe("Linkfav");
Meteor.subscribe("LinkLike");
Meteor.subscribe("LinkComment"); */
$("body").removeClass('home');
this.render('profile');
setTimeout(function(){
$('#username').html(Session.get('first_name'));
$('#profile_username').html(Session.get('first_name'));
$('#setting_name').val(Session.get('first_name'));
$('#setting_username').val(Session.get('first_name'));
$('#setting_email').val(Session.get('email'));
$('#user_id').val(Session.get('id'));
$('.setting_day').val(Session.get('day'));
$('.setting_month').val(Session.get('month'));
$('.setting_year').val(Session.get('year'));
if(Session.get('image')!= ''){
$('.user_profile_image').attr("src",Session.get('image'));
}
if(Session.get('gender') == 0){
$('#user_gender').html('Male');
}else{
$('#user_gender').html('Female');
}
$('#day').html(Session.get('day'));
$('#month').html(Session.get('month'));
$('#year').html(Session.get('year'));
},100);
},onBeforeAction:function(){
if(Session.get('email')){
this.next();
}else {
//this.next();
this.redirect('/');
}
}
});
【问题讨论】:
-
两个流星调用,然后在第二个流星调用中循环?这将非常缓慢、异步且有问题。你想做什么?看起来您正在尝试为您的模板构建数据上下文,但是以一种非常非流星的方式。
-
@MichelFloyd 是对的。如果您需要从存储在 Mongo.Collection 中的服务器上传一些数据(如 cmets,在您的情况下),您应该使用发布/订阅 API。通常你会订阅 Template.profile.onCreated 或 waitOn 回调,以防你使用 iron:router 包。以 MDG 的待办事项列表为例:github.com/meteor/simple-todos.
标签: meteor