Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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
Templates 如果数据更改,则更新meteor模板_Templates_Meteor_Meteor Blaze - Fatal编程技术网

Templates 如果数据更改,则更新meteor模板

Templates 如果数据更改,则更新meteor模板,templates,meteor,meteor-blaze,Templates,Meteor,Meteor Blaze,我有城市流行地名列表的blaze模板: <template name="city"> {{#each places}} {{this}} {{/each}} </template> 但它不起作用,因为在包含数据的助手数组之前呈现的模板已准备就绪。我应该怎么做才能使helper返回的数据成为被动的,并在模板中显示这些数据?使用onRendered钩子将结果分配给模板实例上的被动变量: import { ReactiveVar } from 'meteor/

我有城市流行地名列表的blaze模板:

<template name="city">
  {{#each places}}
    {{this}}
  {{/each}}
</template>

但它不起作用,因为在包含数据的助手数组之前呈现的模板已准备就绪。我应该怎么做才能使helper返回的数据成为被动的,并在模板中显示这些数据?

使用onRendered钩子将结果分配给模板实例上的被动变量:

import { ReactiveVar } from 'meteor/reactive-var';
Template.city.onRendered(function() {
  const self = this;
  self.places = new ReactiveVar();
  service.radarSearch(request, function(points) {
    self.places.set(places);
  }
});
从这里开始,只需从帮助器返回被动变量:

Template.city.helpers({
  places: function() {
    const tpl = Template.instance();
    const places = tpl && tpl.places.get();
    return places || [];
  }
});

在这种情况下,您可能甚至不需要等待模板实例被呈现,
onCreated
就足够了。诚然,
onCreated
也可以工作,但我不想用
onCreated
v
onRendered
的解释来混淆视听。
Template.city.helpers({
  places: function() {
    const tpl = Template.instance();
    const places = tpl && tpl.places.get();
    return places || [];
  }
});