These items need documentation please #406

Closed
bevanhunt opened this Issue Oct 20, 2012 · 6 comments

3 participants

@bevanhunt

#1 - Using jQuery on the client

Template.timer.rendered ->
  $('#timer').css('paddingTop', '20px')

#2 - Globals on the server

GLOBAL.timer = timer

#3 - Short-circuiting required to return collection values on the client

Template.timer.time ->
  timer = Timer.findOne({clock: 1})
  timer and timer.time
@athiwatc

And you just refer the first 3 issue of meteor :)
Anyways, for your first example you should use Template.myTemplate.rendered instead.

@bevanhunt

Thanks @athiwatc - I updated the first item.

@glasser
Meteor Development Group member

I'm not sure what you're getting at with 3. Can you be a little more explicit?

Are you just saying that the Meteor docs should explain that when functions return null, you should check that before dereferencing the return value? That seems like a basic JavaScript concept and it would seem like we'd have to add that in hundreds of places in our docs if we can't assume that users know it. (BTW, you probably want to use findOne() instead of find().fetch()[0].)

@glasser
Meteor Development Group member

Also, I would not necessarily recommend the use of globals on the server at all. Meteor is deliberately vague as to what its execution environment is. Server code should mostly consist of startup code (either inline or in Meteor.startup), method implementations, and publish handlers. Meteor doesn't particularly promise that there will only be one instance of a server running at a time --- if you want to design your apps in a way that they will automatically scale horizontally, you probably don't want to be depending on in-app global state!

@bevanhunt

Thanks @glasser - I replaced the global with fibers to workaround clearing the timer. Mini-Mongo find doesn't work in startup - should it? Regarding issue #3 - short-circuiting is required for Mini-Mongo find on Template helpers due to client-server database sync lag on startup - this could be mentioned with an example in the Template helpers section.

Mini Mongo:

Timer = new Meteor.Collection('timer')
if Meteor.isServer
  Meteor.methods
    'timer': -> Timer.findOne({clock:1})
  Meteor.startup ->
    Timer.insert({clock: 1, time: 10})
    Timer.insert({clock: 2, time: 20})
if Meteor.isClient
  Meteor.startup ->
    Timer.findOne({clock:1}) (returns undefined)
    Timer.find({clock:1}) (returns all items)
    Meteor.call 'timer', (err, data) -> (callback allows access to values)

Globals:

Meteor.methods
  'reset': -> Meteor.clearInterval(GLOBAL.timer)
  'countdown': -> 
    GLOBAL.timer = Meteor.setInterval -> 
      timer = Timer.findOne({clock: 1})
      if timer.time > 0
        Timer.update({clock: 1}, {$inc: {time: -1}})
   , 1000

Fibers:

Meteor.methods
  'countdown': ->
    countdown = ->
      fiber = Fiber.current
        Meteor.setTimeout ->
          Timer.update({clock: 1}, {$inc: {time: -1}})
          fiber.run()
        , 1000
      Fiber.yield()
    timer = Timer.findOne({clock: 1})
    fiber = Fiber ->
      time = timer.time
      while time > 0
        countdown()
        time -= 1
    fiber.run()
@glasser
Meteor Development Group member

OK. So I think the main issue here is that it isn't immediately clear that templates may get rendered before the initial batch of data arrives from the server. We want to add some sort of reactive function that lets you know when your initial batch of data is in; the tracking issue for that is #273.

@glasser glasser closed this Nov 1, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment