A_L
A_L

Reputation: 1146

Global function for Meteor template helper

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.

Upvotes: 12

Views: 14695

Answers (4)

Michel Floyd
Michel Floyd

Reputation: 20256

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.

Upvotes: 6

Tarang
Tarang

Reputation: 75975

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;
});

Upvotes: 20

Abdullah Dahmash
Abdullah Dahmash

Reputation: 535

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

Upvotes: 8

JimHough
JimHough

Reputation: 162

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);

Upvotes: 13

Related Questions