Subscription Handle for Meteor.users

893 views
Skip to first unread message

Ian Serlin

unread,
Jun 4, 2013, 2:15:01 PM6/4/13
to meteo...@googlegroups.com

I would like to have access to the Subscription handle associated with the Meteor.users collection the same as if I had done Meteor.subscribe('users').

The reason is that I want to be able to check the .ready() property of the subscription handle to know when the users collection has been synced on startup in a clean way. Sure you can do {{#if currentUser}}, etc. but that's really not good enough to do control flow in a complex app. I can't have all my routing logic in templates.

Since the users collection is autopublished to the client by the accounts-base package I don't see a way to get a handle, because even if I did an additional 

client: handle = Meteor.subscribe('myUsers'); handle.ready() ?

server: Meteor.publish('myUsers', function(){ return some users });

that doesn't guarantee the built-in subscription is ready even though it's a reasonable assumption.

Thoughts?

The relevant code is in /packages/accounts-base/accounts_server.js:

~~~~~

// Publish the current user's record to the client.

Meteor.publish(null, function() {

  if (this.userId) {

    return Meteor.users.find(

      {_id: this.userId},

      {fields: {profile: 1, username: 1, emails: 1}});

  } else {

    return null;

  }

}, /*suppress autopublish warning*/{is_auto: true});

...

  Meteor.default_server.publish(null, function () {

    if (this.userId) {

      return Meteor.users.find(

        {_id: this.userId},

        {fields: toFieldSelector(Accounts._autopublishFields.loggedInUser)});

    } else {

      return null;

    }

  }, /*suppress autopublish warning*/{is_auto: true});


  // XXX this publish is neither dedup-able nor is it optimized by our

  // special treatment of queries on a specific _id. Therefore this

  // will have O(n^2) run-time performance every time a user document

  // is changed (eg someone logging in). If this is a problem, we can

  // instead write a manual publish function which filters out fields

  // based on 'this.userId'.

  Meteor.default_server.publish(null, function () {

    var selector;

    if (this.userId)

      selector = {_id: {$ne: this.userId}};

    else

      selector = {};


    return Meteor.users.find(

      selector,

      {fields: toFieldSelector(Accounts._autopublishFields.otherUsers)});

  }, /*suppress autopublish warning*/{is_auto: true});

});


~~~~~

Daniel Dornhardt - Daniel Dornhardt Development

unread,
Jun 4, 2013, 3:11:11 PM6/4/13
to meteo...@googlegroups.com
Hi,

Accounts.loginServicesConfigured() tells you if a potentially user has been initialized (if he has the logged in tokens yadda yadda) or if not, then as soon as the system knows that.

So it's about as good as the handle.ready() and you can query the rest of the collection with the Meteor.user* - methods.

I *REALLY* think this should be documented, maybe next to the subscription handle explanation in the docs and also in the Accounts docs.

I don't know why it's not part of the public API yet because I think it's something everybody trying to represent a full display of all the known data to the user as a whole experience is gonna stumble upon.

Best wishes

Daniel

Daniel Dornhardt, Daniel Dornhardt Development
+49 152 - 56 17 22 61


--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Avital Oliver

unread,
Jun 4, 2013, 11:51:43 PM6/4/13
to meteo...@googlegroups.com
Does `!Meteor.loggingIn()` do the trick?


--

Ian Serlin

unread,
Jun 5, 2013, 12:19:13 AM6/5/13
to meteo...@googlegroups.com
Happily, yes although it's semantically different. Thank you Avi.

--
You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/QKXe7qfBfqg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages