[英]Meteor re-render template after user login
Ok so im using Facebook API graph when the user is logging in using facebook im getting the user friend list + friends pictures like that: 好的,所以当用户使用Facebook登录时,我会使用Facebook API图,这样获得用户朋友列表+朋友图片:
Template.user_loggedout.events({
"click #fb": function (e, tmp) {
Meteor.loginWithFacebook({
requestPermissions: ['user_likes',
'friends_about_me',
'user_birthday',
'email',
'user_location',
'user_work_history',
'read_friendlists',
'friends_groups',
'user_groups']
}, function (err) {
if (err) {
console.log("error when login with facebook " + err);
} else {
FB.api('/' + Meteor.user().services.facebook.id + '/friends', { fields: 'name,picture' }, function (response) {
if (response && response.data) {
friends = response.data
}
})
}
});
}
and as expected i get friends array inside there are objects(represented as friends). 和预期的一样,我在里面得到了friends数组,有一些对象(表示为friends)。 at some point when the user clicks a link i want to show the user his friends list.
在某个时候,当用户单击链接时,我想向用户显示他的朋友列表。 so i got this html:
所以我得到了这个HTML:
Template.add_friends.helpers({
friends_list: friends
});
template name="add_friends">
<div class="friends_Page">
{{#each friends_list}}
<li>{{name}}</li>
{{/each}}
</div>
</template>
the problem is that the friends object gets updated after the app as started and when the user clicked the login button using facebook. 问题是在启动应用程序后以及用户使用Facebook单击登录按钮时,friends对象会更新。 so how to re-render when i get the callback from facebook graph api?
所以当我从facebook graph api获得回调时如何重新渲染? i dont want to store the friends in database (its already available thanks to facebook )
我不想将朋友存储在数据库中(由于facebook,它已经可用)
The most natural way to force a redraw when data is changed is to use a dependency on that data. 更改数据时强制重绘的最自然方法是使用对该数据的依赖性。
var friends = [];
var friends_dep = new Deps.Dependency();
Template.myTemplate.friends_list = function() {
friends_dep.depend(); /* Causes the helper to rerun when data changes */
return friends;
};
...
function callback() {
...
friends = response.data;
friends_dep.changed(); /* Call this after you set the new data */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.