【问题标题】:How do I do an analog of a page scope in variable in Meteor?如何在 Meteor 的变量中模拟页面范围?
【发布时间】:2015-06-17 15:12:44
【问题描述】:

我有这个 Meteor 布局:

<template name="layout">
    {{> toolbar }}

    <div class="container">
        {{> yield}}
    </div>

</template>




Router.configure({
  layoutTemplate: 'layout'
});

我想让工具栏参数根据页面显示动态内容。我可以通过使用一些反应变量来做到这一点。但是,当同一个客户端同时打开多个页面时会发生什么?每个页面的工具栏都应该显示自己的内容,彼此独立。如何实现?

【问题讨论】:

  • 类似Template.layout.helpers({toolbar: function(){return 'some value'}})?
  • 你可以用任何你想要的东西替换'some value'
  • 如何从另一个模板(通过 {{yield}} 调用的模板)访问此帮助器?
  • 要使用 registerHelper,请参阅 docs.meteor.com/#/full/template_registerhelper

标签: meteor meteor-helper


【解决方案1】:

如果您打算在同一页面的多个模板中使用响应式数据,我建议您声明一个响应式变量 (ReactiveVar()) 或 reactive dictionary

在您的 js 文件的开头(如果有多个,请使用父模板文件),在任何 template.yourTemplate.renderedtemplate.yourTemplate.rendered 之外,您声明它。范围将涵盖页面内调用的所有模板。

一个反应式字典的例子:

var pageSession = new ReactiveDict();

在你的主模板rendered 函数上,你设置你的反应变量:

pageSession.set("yourVariable", yourValue);

你设置了一个帮助器来返回它:

Template.layout.helpers({
    toolbar: function(){
        return pageSession.get("yourVariable");
    }
})

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多