reactive template helper

277 views
Skip to first unread message

mike_haney

unread,
Jul 30, 2014, 5:49:07 AM7/30/14
to meteo...@googlegroups.com
i've tried searching and doing quite a bit--i'm having a helluva time. my problem is simple:

i have an object that has a property called "startTime". it's a unix timestamp. in the template js i have a helper that gets the current client system time and figures out the time since the object's startTime and returns that to be displayed on the page. this works for the initial page load, but it seems that no matter what i do, i can't make this data reactive. i've tried using deps and autorun to contain the chunk of code. i've tried using the ReactiveDict class to store the data and update the template. i've also tried using Meteor.setInterval to update this data. One thing I see thrown around (too) often is: "use a session variable". i feel like that's poor design, and would rather avoid doing that. basically, what i need is the template rendering to be re-done every second or so--or, barring that, the chunk of html represented by my template helper to be updated every second or so.

Jay Greenspan

unread,
Jul 30, 2014, 10:45:22 AM7/30/14
to meteo...@googlegroups.com
What's your objection to Session variables? I haven't run across objections to them before.

A setup like this wouldn't work for you?


<template name="showTimer">
    <h1>{{ elapsedTime }}</h1>
    <button id="startTimer">Start</button>
</template>

And then in helpers:


Template.showTimer.helpers({
    return Session.get('elapsedTime');
})

Template.showTimer.event({
    'click #startTimer: function(){
        startTimer();
    }
})

function startTimer(){
    //calculate elapsed time... 
   Session.set('elapsedTime', calculatedTime);
}

In response to a question I posed a few months ago Robert Böhm, wrote these functions, which accurately calculate elapsed time:

mike_haney

unread,
Jul 30, 2014, 5:19:55 PM7/30/14
to meteo...@googlegroups.com
my objection to using session variables is the answer to the question: "does this need to be stored globally?". it feels like a poor solution to just stick things into global storage when it seems like i can get it to work more properly. the solution you posted wouldn't work, because i have a list of n objects that have the startTime property. i could modify the key slightly to be something like object._id + ".startTime", but that feels like a poor solution. i tried using the ReactiveDict object, but it seemed to not update the variable more than once.

from mongo, but it feels like a poor solution to just stick

David Greenspan

unread,
Jul 30, 2014, 5:41:45 PM7/30/14
to meteo...@googlegroups.com
My preferred pattern is to have a single global reactive variable that stores the current time, updated by a setInterval every 10 seconds or so.  Then, have all the "time ago" helpers use this variable in their calculations.  That way, you only have one timer, not lots of timers, and you don't have to stop and start timers.

ReactiveDict should work just the same as Session, since Session is just a ReactiveDict.  There's also Blaze.ReactiveVar, which I intend to eventually work into the public API as a template-local reactive variable.

-- David



--
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/d/optout.

Michael Haney

unread,
Jul 30, 2014, 5:49:47 PM7/30/14
to meteo...@googlegroups.com
oh very nice! that is actually a great idea david! 


--
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/6datYopVMrE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.

mike_haney

unread,
Jul 30, 2014, 7:11:34 PM7/30/14
to meteo...@googlegroups.com
solved all the problems! the current time is a thing worthy of storing globally, so i like putting it in session. it's also capable of tracking that my template helper uses it, so it updates all the object's templates! genius! thanks a lot david! (sorry, i'm only like 10 days into this whole web programming thing)
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
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/6datYopVMrE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to meteor-talk+unsubscribe@googlegroups.com.

David Greenspan

unread,
Jul 30, 2014, 7:18:31 PM7/30/14
to meteo...@googlegroups.com
No problem, glad you like it!

-- David



To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages