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

前端 未结 5 1803
无人共我
无人共我 2020-12-25 09:52

I have a helper like

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


        
相关标签:
5条回答
  • 2020-12-25 10:36

    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!

    0 讨论(0)
  • 2020-12-25 10:38

    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}}
    
    0 讨论(0)
  • 2020-12-25 10:41

    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 讨论(0)
  • 2020-12-25 10:42

    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":"";
    });
    
    0 讨论(0)
  • 2020-12-25 10:46

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

    0 讨论(0)
提交回复
热议问题