簡體   English   中英

從Meteor中的onCreated訪問助手

[英]Access helpers from onCreated in Meteor

我有:

Template.myTemplate.helpers({
  reactiveVar: new ReactiveVar
});

如何從onCreated訪問reactiveVar進行設置?

Template.restaurantEdit.onCreated(function() {
  // Access helpers.reactiveVar from here to set up
  // My goal is to set up data to reactiveVar, ex:
  helpers.reactiveVar = this.data.someData;
});

我發現有受保護的__helpers: this.view.template.__helpers

但有沒有Meteor訪問助手的好方法? 或者是Meteor從加載data設置reactiveVar的方法

你基本上不直接訪問Meteor中的助手。 如果你想使用與ReactiveVar的范圍反應 ,你應該這樣做:

Template.restaurantEdit.onCreated(function() {
  //define all your reactive variables here
  this.reactiveVar = new ReactiveVar('default value');
});

Template.restaurantEdit.helpers({
  reactiveVar: function() {
    //access reactiveVar template variable from onCreated() hook
    return Template.instance().reactiveVar.get();
  }
});

Template.restaurantEdit.events({
  'click yourselector': function(evt, template) {
    template.reactiveVar.set('new value');
  }
});

在這里閱讀更多關於范圍反應性的信息: https//dweldon.silvrback.com/scoped-reactivity

您應該在onCreated()塊中設置反應變量,然后通過helper訪問它,反之亦然。

Template.restaurantEdit.onCreated(function(){
  this.foo = new ReactiveVar;
});

Template.restaurantEdit.helpers({
  foo: function(){
    return Template.instance().foo.get();
  }
});

只要您的ReactiveVar發生更改,幫助程序就會更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM