Meteor Snippet Catrgory

I’d like to suggest a “snippet” for Meteor Forums. There are so many useful little snippets that would be great to document somewhere.

For example, I just spent about 15 minutes trying to figure out how to keep track of user sessions and it was pretty simple, but I could have saved myself 15 minutes if there were just a snippet somewhere:

debug = console.log.bind(console, 'session')
# debug = (->)

Accounts.onLogin ({user, connection}) ->
  userId = user._id
  start = timestamp()
  debug('start', userId, start)
  connection.onClose ->
    end = timestamp()
    debug('stop', userId, end)
    Session.insert({start, stop, userId})

Also, here’s a badass progress bar implementation!

  Meteor.publish('posts', function() {
    var pub = this
    var cursor = Posts.find()
    var collectionName = cursor._cursorDescription.collectionName
    var total = cursor.count()
    var i = 0
    cursor.observeChanges({
      added: function (id, fields) {
        fields.subId = pub._subscriptionId
        i++
        fields.progress = i
        fields.total = total
        Meteor._sleepForMs(100)
        pub.added(collectionName, id, fields);
      },
      changed: function (id, fields) {
        pub.changed(collectionName, id, fields);
      },
      removed: function (id) {
        pub.removed(collectionName, id);
      }
    })
  })

I just think it would be awesome if I could search for these things because every Meteor developer comes up with interesting ways of doing things.

2 Likes

Good idea, would be fun developing, but I dont see myself moderating it :smiley:

BTW, how to structure it?
Some prepared category structure plus custom tags ?

Looks like you’re halfway to a package. Some packages I use contains even less code than your progress bar. And a package can be updated, maintained, you can raise issues on it etc… whereas with a snippet you’d have to create your own convention of updating it, submitting to it, categorising it etc…

When you submit a post on this forum, you can have a category such as “announce” or “help”, etc. I was thinking it would just be a category on this forum.

I suppose it could be a package, but it bugs me how some really simple packages can get so large and generic when thats not what you need. Often times, Id much rather just understand how something works and build it myself tailored to what I want…

I’m thinking a category on this forum, but very unstructured. Really, just a way of searching. And you’ll get to find out about neat ways of doing things like the snippets above!

Interesting, at times I’ve wanted to contribute snippets of code too, the closest I could find is: http://themeteorchef.com/snippets/
But having a snippets tag here, unmaintained, would be a nightmare to search through with potentials lots of duplicates that does the same/similar thing with a crude (like) voting system… somewhat like the packages system atm hehe
Anyway, just voicing some concerns, would like to see some discussions as I think newer members (like me) will find this very useful.
I can already think of a few commonly asked questions that’ll fit into snippets:

  • pub/sub to a collection
  • structure my dir
  • nest blaze in react
  • passing data to child template
  • lots of autoforms related Qs

By the way your publish needs to call this.ready() after the observer and make sure that your observer is stopped onStop otherwise it will run forever.

3 Likes

It think thats fine. Thats a beauty of search :slight_smile: