javascript Meteor,如何从另一个助手访问助手?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17229302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:33:34  来源:igfitidea点击:

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

javascriptmeteorhandlebars.js

提问by Jonathan de M.

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 userhelper and compare its _idwith the current user _id, to tell whether the user is visiting its own profile. I'm using something pretty ugly:

我想向集合中添加一个助手,该助手可以访问该user助手并将其_id与当前用户进行比较_id,以判断用户是否正在访问其自己的个人资料。我正在使用一些非常难看的东西:

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?

有没有更好的方法来访问另一个助手?

回答by outluch

I've just accidentally discovered this in the console:

我只是在控制台中偶然发现了这一点:

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

So, Blaze._globalHelpersis what we are looking for!

所以,Blaze._globalHelpers这就是我们要找的!

回答by avalanche1

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

您可以使用以下命令调用模板助手(不是全局助手 - 这是 outluch 的答案):

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

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

MDG 建议使用常规函数,然后将其传递给助手、事件等。见这里

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

16 年 6 月 16 日更新
实际上我强烈建议简单地使用manuel:viewmodel- 它减轻了许多 Blaze 的头痛......

回答by Fran?ois

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

当我正在寻找一种从另一个助手调用助手的方法时,我发现 Meteor 1.0 定义了可供所有其他助手使用的“Template.registeredHelpers”。https://docs.meteor.com/#/full/template_registerhelper

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

回答by Brian Bolton

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

你甚至可能不需要像那样打电话给帮手。已经内置了一个 currentUser 助手。

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

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

{{currentUser}}

回答by GeriTol

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