最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Meteor, how to access to a helper from another helper? - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question edited Dec 25, 2014 at 21:43 Mahesh Babu 1451 silver badge14 bronze badges asked Jun 21, 2013 at 6:52 Jonathan de M.Jonathan de M. 9,8088 gold badges49 silver badges72 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 34

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!

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...

As I was searching for a way to call a helper from another helper, I found that Meteor 1.0 defines "Template.registeredHelpers" that are available for all other helpers to use. https://docs.meteor.com/#/full/template_registerhelper

Template.registerHelper("checkedIf",function(value){
  return value?"checked":"";
});

You might not even need to call a helper like that. There is a currentUser helper already built in.

http://docs.meteor.com/#template_currentuser

{{currentUser}}

maybe this would work for you:

  //js
Template.foo.helpers({ bar: function() {
 return this.userId == Meteor.userId(); },
 domain: function() {
 var a = document.createElement('a'); a.href = this.url;
 return a.hostname;
 } });

 ownsDocument = function(userId, doc) { return doc && doc.userId === userId;}

 Posts = new Meteor.Collection('posts');
 Posts.allow({
 update: ownsDocument, remove: ownsDocument
 });

  //html
{{#if bar}}<a href="{{pathFor 'postEdit'}}">Edit</a>{{/if}}
发布评论

评论列表(0)

  1. 暂无评论