Meteor 订阅和显示用户数 [英] Meteor Subscribe and Display Users Count

查看:23
本文介绍了Meteor 订阅和显示用户数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的页脚中显示用户数量,我想实时显示这个数字.我认为正确的方法是在服务器中创建一个发布并从客户端订阅.

I'm trying to display the number of users in my footer and I would like this number in real time. I think the proper way is to create a publication in the server and to subscribe from the client.

// server/publications.js
Meteor.publish("usersCount", function () {
    return Meteor.users.find();
});

// client/main.js
UsersCount = new Meteor.Collection("usersCount");
Meteor.subscribe('usersCount', [], function() {
    console.log('subscribed.');
});

// client/views/layout/footer.js
Template.footer.helpers({
    famecoiners: function(){
        return UsersCount.find().count();
    }
});

// client/views/layout/footer.html
<span>{{famecoiners}} Famecoiners!</span>

在 chrome 控制台中,我们可以看到来自回调函数的 'subscribed' 字符串.问题是:{{famecoiners}} 在我的模板中总是返回 0.

In the chrome console, we can see the 'subscribed' string from the callback function. The problem is: {{famecoiners}} always returns 0 in my template.

推荐答案

发布所有用户是可行的,但不切实际.如果您有 10,000 个用户怎么办?您真正想要做的是创建一个客户端集合,其中仅包含一个表示计数的项目.如果您查看文档的 发布和订阅 部分中的第二个示例,您会看到一些内容这样做.我将在下面展示一个完整的工作示例:

Publishing all of the users will work, but it isn't practical. What if you have 10,000 users? What you really want to do is create a client side collection that holds only one item representing the count. If you look at the second example in the Publish and subscribe section of the docs, you'll see something that does this. I'll present a complete working example below:

$ meteor create test
$ cd test
$ rm test.*
$ mkdir server client
$ meteor remove autopublish
$ meteor add coffeescript

client/client.coffee

# Create a client-side subscription for the count of users.
UsersCount = new Meteor.Collection 'users-count'

Meteor.subscribe 'usersCount'

Meteor.startup ->
  Meteor.setInterval (->
    # To show something working, read the users count every second.
    uc = UsersCount.findOne()
    console.log uc.count or 0
  ), 1000

server/server.coffee

# Create a 'Users' group for this demo instead of using accounts-password.
Users = new Meteor.Collection 'users'

Meteor.publish 'usersCount', ->
  count = 0 # the count of all users
  initializing = true # true only when we first start
  handle = Users.find().observeChanges
    added: =>
      count++ # Increment the count when users are added.
      @changed 'users-count', 1, {count} unless initializing
    removed: =>
      count-- # Decrement the count when users are removed.
      @changed 'users-count', 1, {count}

  initializing = false

  # Call added now that we are done initializing. Use the id of 1 since
  # there is only ever one object in the collection.
  @added 'users-count', 1, {count}
  # Let the client know that the subscription is ready.
  @ready()

  # Stop the handle when the user disconnects or stops the subscription.
  # This is really important or you will get a memory leak.
  @onStop ->
    handle.stop()

Meteor.startup ->
  # To show something working, insert a new user ever 0.5 seconds.
  Meteor.setInterval (-> Users.insert {}), 500

抱歉,如果您不喜欢 CoffeeScript - 我希望代码简洁.

Sorry if you don't like the CoffeeScript - I wanted the code to be compact.

$ meteor start

如果您打开控制台,您将看到用户数增加.为了好玩,您可以打开 mongo shell 并运行 db.users.remove(); 并观察它重置.

If you open the console you will see the user count increase. For fun you can open the mongo shell and run db.users.remove(); and watch it reset.

这篇关于Meteor 订阅和显示用户数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆