Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从帮助器获取模板数据_Javascript_Meteor_Meteor Blaze - Fatal编程技术网

Javascript 从帮助器获取模板数据

Javascript 从帮助器获取模板数据,javascript,meteor,meteor-blaze,Javascript,Meteor,Meteor Blaze,我正在尝试在OnCreate挂钩上更新模板的datacontext: Template.segment.onCreated(function () { var tp = this; this.test = "TEST"; Meteor.call('getTreeCategData', function (error, data) { tp.ExternalapiData = data; tp.chart = new Chart(); //... });

我正在尝试在OnCreate挂钩上更新模板的datacontext:

Template.segment.onCreated(function () {
  var tp = this;

  this.test = "TEST";
  Meteor.call('getTreeCategData', function (error, data) {
    tp.ExternalapiData = data;
    tp.chart = new Chart(); 
    //...
  });
});
我想在父模板上保留这些引用,以便从childs模板访问它们

现在,我使用onCreate上设置的值“更新”datacontext

Template.segment.helpers({
  parentDatacontext: function(){
    this.chart = Template.instance().chart; //undefined
    this.apiData = Template.instance().apiData; //undefined
    //But it executed later, when I open the chrome object inspector    the reference, they are not undefined anymore 
    console.log("parentDatacontext: ", Template.instance());
    return this;
  }
});
我的模板:

<template name="segment">
{{#with parentDatacontext}}
{{> childtp}}
{{/with}}
</template>
实际上,我很快就被这个过程卡住了,我甚至无法打印从帮助器创建的onCreate上添加的属性集。。。 我猜助手是在创建结束之前由模板调用的

你知道如何解决这个问题,或者有更好的方法来做我想做的事情吗

感谢

parentDataContext()
在您的
Meteor.call返回之前被执行,然后由于您的变量没有反应,帮助器不会再次执行。您希望使用存储这些值,以便在方法设置值时执行帮助程序

要访问子模板中的新被动变量,请跟随。

如何

和模板代码

<template name="segment">
    {{#with parentDatacontext}}
        {{> childtp}}
    {{else}}
        Loading or something...
    {{/with}}
</template>

{{#带parentDatacontext}
{{>childtp}
{{else}
装东西什么的。。。
{{/与}}

实际上似乎工作得很好,请在我验证之前给我一些时间检查一下!谢谢:)是否要写入if(!this.apiData)以检查无数据?我认为undefined在Js中是错误的?如果存在
this.apiData
,我们返回
this
-这不是错误的值,所以
{{{with…}}
块将被渲染-如果现在有
apiData
,助手返回
undefined
{else}
块被渲染。Ho yep对不起,我的坏消息,你是真实的化身!你也是对的,但是@Radoslaw M的答案上有代码示例,所以我验证了他。但你的答案是互补的,请给出原因,谢谢
Template.segment.onCreated(function () {
  var tp = this;

  this.apiData = new ReactiveVar();
  Meteor.call('getTreeCategData', function (error, data) {
    tp.apiData.set(data);
    tp.chart = new Chart();
    //...
  });
});

Template.segment.helpers({
  parentDatacontext: function(){
    this.chart   = Template.instance().chart;
    this.apiData = Template.instance().apiData.get();

    //    data - {{with parentDatacontext}}
    // no data - {{else}}
    if (this.apiData) {
        return this;
    }
  }
});
<template name="segment">
    {{#with parentDatacontext}}
        {{> childtp}}
    {{else}}
        Loading or something...
    {{/with}}
</template>