Global function for Meteor template helper
Asked Answered
A

4

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.

Alliber answered 19/12, 2013 at 12:25 Comment(0)
V
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;
});
Vladamir answered 19/12, 2013 at 12:32 Comment(3)
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?Alliber
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 thereVladamir
In this case, how would dialogBoxOptions gain access to, say, a Collection subscription within the Template instance?Animus
S
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);
Sparkman answered 11/7, 2014 at 12:39 Comment(4)
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?Animus
@Aaron: Template.registerHelper would be the correct method: docs.meteor.com/#/full/template_registerhelperClip
@Clip I'm now using the registerHelper(s). thank you. :)Animus
Bit of a hack, but after Template.registerHelper({...}), you can get the function from Blaze._globalHelpers['func_name'].Evonevonne
C
8

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

Catechumen answered 21/12, 2014 at 19:24 Comment(0)
R
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.

Rabiah answered 16/1, 2015 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.