Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor, how to access to a helper from another helper?

I have a helper like

Template.user_profile.helpers({   user:function() {      return Meteor.users.find({'profile.front_name':Session.get('slug')}).fetch()[0];   } }); 

I want to add a helper to the collection which could access the user helper and compare its _id with the current user _id, to tell whether the user is visiting its own profile. I'm using something pretty ugly:

Template.user_profile._tmpl_data.helpers.user() 

The final code:

Template.user_profile.helpers({   user:function() {      return Meteor.users.find({'profile.front_name':Session.get('userId')}).fetch()[0];   },   isCurrentUser: function() {     return Template.user_profile._tmpl_data.helpers.user()._id === Meteor.userId();   } }); 

Is there any better way to access another helper?

like image 565
Jonathan de M. Avatar asked Jun 21 '13 06:06

Jonathan de M.


People also ask

What is helpers in Meteor JS?

meteor Blaze Templating Template Helpers Template helpers are an essential part of Blaze and provide both business logic and reactivity to a Template. It is important to remember that Template helpers are actually reactive computations that are rerun whenever their dependencies change.

What are helper methods in JavaScript?

Helper functions are JavaScript functions that you can call from your template. Ember's template syntax limits what you can express to keep the structure of your application clear at a glance. When you need to compute something using JavaScript, you can use helper functions.


2 Answers

I've just accidentally discovered this in the console:

Template.registerHelper function (name, func) {                                                                                Blaze._globalHelpers[name] = func;                                                                                    }  

So, Blaze._globalHelpers is what we are looking for!

like image 144
outluch Avatar answered Oct 02 '22 07:10

outluch


You can call a template helper (not global helper - which is in outluch's answer) with:

Template.tplName.__helpers.get('helper').call() 

MDG suggests using a regular function and then passing it to helpers, events and so on. See here.

Update 16.06.16
Actually I strongly advise to simply use manuel:viewmodel - it alleviates so many Blaze headaches...

like image 20
avalanche1 Avatar answered Oct 02 '22 07:10

avalanche1