Meteor Global Helpers – Tips & Tricks

Meteor Tips & Tricks

This post is part of my Meteor Tips & Tricks series, which are short posts covering helpful meteor tips and tricks.

One of the things that I really like about Meteor is the Blaze / handlebars template structure and how data binding works. I like how easy it is to setup template helpers, the on render function and events. But I have noticed that across different templates and views I’m doing a number of  helpers functions. This is where Meteor global helpers or handlebars helpers come into play.

I’m going to cover a number of helpers that I have created and been using in my meteor production apps. Disclaimer there might be a better way to do this or better helpers for what I’m doing but for me this seems to work just fine and I like to keep things simple.

In this post I’m only going to cover client side helpers and when creating your own helpers make sure to put them inside your client folder under a helpers folder like so

Meteor Global Helpers - Tips & Tricks

 

toFixedDecimal

The first helper I’m going to cover is Decimal To Fixed which is a simple helper that takes any float number and runs the toFixed() method. Noticed if my context is 1.00 or 2.00 I don’t want to return a float number but just 1 or 2.

Now using the helper is super easy I call the toFixedDecimal UI helper function pass the number and the fixed point which in this case is 2

checkedRadioButton

The next helper is a very simple helper to set selected or “checked” for radio buttons

Again nothing to complex passing the current value and expected value if they match return checked

timeHelper

Here I have a number of global helper functions for formatting time, these helpers require the moment.js meteor package

The first helper formatTime takes the time and then the format you want it returned based on moment.js formats

The rest are very simple and self explanatory, if you have any questions send me a comment. Don’t forget to check my recommend meteor packages Common meteor packages I use in my apps and development

Sign me up for the newsletter!

Related Post

Leave a Comment