创建一个通用辅助变量

时间:2017-01-30 00:09:27

标签: meteor meteor-blaze

有没有办法在模板助手的顶部创建一个变量来删除重复。

在这种特殊情况下,我正在使用var candidate = FlowRouter.getParam('id');,我必须在每个帮助器中创建变量。我认为有更好的方法。

professionalOverview: function() {
    var candidate = FlowRouter.getParam('id');
        return ProfessionalOverview.findOne({ candidateUserId: candidate });
    },
  candidateImg: function() {
    var candidateUserId = FlowRouter.getParam('id');

    return Files.findOne({ userId: candidateUserId });
  },

修改

Template.talentProfileNew.onCreated(function() {
    var self = this;
    self.autorun(function(){
        this.candidateUserId = new ReactiveVar(FlowRouter.getParam('id'));
    }
});

Template.talentProfileNew.helpers({
  candidate: function() {
    console.log(Template.instance().candidateUserId.get());

    return Meteor.users.findOne({_id: Template.instance().candidateUserId.get()});
  }
});

1 个答案:

答案 0 :(得分:1)

你可以在onCreated()中读取一次并将其放入反应变量中。 e.g。

< div class="cta filter" >
  < a class="all active" data-filter="all" href="#all" role="button" >All Muses< /a>
  < /br>
  < a class="timelords" data-filter="timelords" href="#timelords" role="button" >Time Lords/Ladies< /a>
   < a class="angels" data-filter="angels" href="#angels" role="button" >Angels< /a>
  < /br>
  < a class="amelia2" data-filter="amelia2" href="#amelia2" role="button">Amelia2< /a>
< a class="amelia3" data-filter="amelia3" href="#amelia3" role="button">Amelia3< /a>
< a class="james" data-filter="james" href="#james" role="button">James< /a>
< a class="rachel" data-filter="rachel" href="#rachel" role="button">Rachel< /a>
< a class="keegant" data-filter="keegant" href="#keegant" role="button">Keegan and T< /a>
< a class="jessica" data-filter="jessica" href="#jessica" role="button">Jessica< /a>
  < a class="henry" data-filter="henry" href="#henry" role="button">Henry< /a>
< a class="johnny" data-filter="johnny" href="#johnny" role="button">Johnny< /a>
< a class="dani" data-filter="dani" href="#dani" role="button">Dani< /a>
< a class="judy" data-filter="judy" href="#judy" role="button">Judy< /a>
< /div>
< div class="boxes" >

< a class="amelia2" data-color="amelia2" >
TEXT TEXT TEXT
< /a>