我在Meteor聊天应用程序中使用at.js' @who提及'(https://github.com/ichord/At.js),并希望执行以下操作:
显示'用户名:firstName lastName'在@who列表中。
它目前只显示用户名。
mongo集合存储profile.firstName中的firstName,profile.lastName中的lastName
channel.js中的当前代码:
setTimeout(function () {
var dbUsers = Meteor.users.find({_id: {$ne: Meteor.userId()}}).fetch();
var users = _.sortBy(dbUsers, 'username');
users = _.pluck(users, 'username');
self.$('textarea[name=message]').atwho({
at: '@',
data: users,
}).on('shown.atwho', function(event) {
setIsMentioning(true);
}).on('hidden.atwho', function(event) {
setTimeout(function() { setIsMentioning(false); }, 100);
});
}, 500);
我在channel.js中的新代码:
setTimeout(function () {
var dbUsers = Meteor.users.find({_id: {$ne: Meteor.userId()}}).fetch();
var users = _.map(dbUsers, function(user) { return _.pick(user, 'username', 'profile.firstName', 'profile.lastName'); });
self.$('textarea[name=message]').atwho({
at: '@',
data: users,
displayTpl: '<li>${firstName} ${lastName}, ${degree}</li>',
insertTpl: ':${username}:'
}).on('shown.atwho', function(event) {
setIsMentioning(true);
}).on('hidden.atwho', function(event) {
setTimeout(function() { setIsMentioning(false); }, 100);
});
}, 500);
以下是users.js中用户的发布:
Meteor.publish('users', function() {
if(this.userId) { // We should only send data to logged in users.
// filter by organization
var user = Meteor.users.findOne(this.userId);
if(user.profile && user.profile.organizationId)
return Meteor.users.find(
{'profile.organizationId': user.profile.organizationId},
{fields: {'username': 1, 'emails': 1, 'status': 1, 'profile.firstName': 1, 'profile.lastName': 1}}
);
else
return Meteor.users.find({}, {fields: {'username': 1, 'emails': 1, 'status': 1}});
}
this.ready();
});
这里是routes.js中的用户订阅:
teamRoutes = FlowRouter.group({
prefix: '/teams/:team',
middlewares: [AccountsTemplates.ensureSignedIn],
subscriptions: function(params) {
this.register('teamChannels', Meteor.subscribe('teamChannels', params.team));
this.register('teamDirectChannels', Meteor.subscribe('teamDirectChannels', params.team));
this.register('users', Meteor.subscribe('users', params.team));
this.register('teams', Meteor.subscribe('myTeams'));
this.register('reads', Meteor.subscribe('reads'));
this.register('mentions', Meteor.subscribe('mentions'));
}
});
非常感谢任何可以向我解释我做错事的人!非常感谢。