12

I have registered a global function like this:

Handlebars.registerHelper('dialogBoxOptions', function (callbackFunctionName){
    return {
        callBack: callbackFunctionName
    };
});

but when I try to access it as below I get dialogBoxOptions is not defined

Template.myLlist.helpers({
    dOpt: dialogBoxOptions('dlgCB')
});

I have tried this as a global handlebars helper and a regular javascript function but get the same result.

A_L
  • 1,116
  • 2
  • 11
  • 25

4 Answers4

20

You can't access handlebars helpers this way you can access them in the template:

<template name="myList">
     {{dialogBoxOptions.callback 'something'}}
</template>

If you want to access it in your helper like you are doing now you should register a global method instead. You could put this in a file like /lib/helpers.js

dialogBoxOptions = function (callbackFunctionName){
    return {
        callBack: callbackFunctionName
    };
}

Also if you want to make a global template helper, the syntax is now:

Template.registerHelper("dialogBoxOptions", function (param2) {
    return true;
});
Tarang
  • 75,157
  • 39
  • 215
  • 276
  • Thanks, I had tried a global javascript function but I think it was in the wrong location, putting in /lib seems to have done the trick. One note: it doesn't work when using "function dialogBoxOptions(callbackFunctionName)", I have to use your syntax - why would that be? – A_L Dec 19 '13 at 12:55
  • 3
    `function xx()` is variable scoped so it works but only in the file its defined in, likewise `var xx = function()` is also scoped to the file. Without the `var` keyword it is visible to all the files. `/lib` is loaded first so perhaps that why it worked after you moved it there – Tarang Dec 19 '13 at 13:38
  • In this case, how would dialogBoxOptions gain access to, say, a Collection subscription within the Template instance? – Aaron Nov 15 '14 at 17:00
13

There is now a way to get access to the registered global helpers.

//Register the helper
UI.registerHelper("functionName", function (param1, param2) {
  return true;
});

//Use the helper elsewhere
var result = UI._globalHelpers('functionName')(param1, param2);
JimHough
  • 162
  • 1
  • 6
  • 2
    Isn't UI.registerHelper depreciated for Template.helpers? What would be the syntax for accessing the Template.helpers in this case? Maybe Template._globalHelpers perhaps? – Aaron Nov 15 '14 at 16:11
  • 7
    @Aaron: `Template.registerHelper` would be the correct method: http://docs.meteor.com/#/full/template_registerhelper – erb Dec 03 '14 at 14:33
  • @erb I'm now using the registerHelper(s). thank you. :) – Aaron Dec 05 '14 at 03:24
  • 4
    Bit of a hack, but after `Template.registerHelper({...})`, you can get the function from `Blaze._globalHelpers['func_name']`. – morsecoder Oct 08 '15 at 15:51
8

Use Template.registerHelper(name, function)
As shown in Meteor Documentation

Abdullah Dahmash
  • 525
  • 1
  • 6
  • 12
6

In Meteor 1.0+ it looks like the syntax for creating a global helper is now:

Template.registerHelper('functionName',function(param1,param2){
  ... your code here ...
})

Then use it anywhere on the client with:

var result = Blaze._globalHelpers.functionName(param1, param2);

OTOH, the UI object doesn't appear in the current documentation so I'm left wondering if this usage is blessed.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39