设置Meteor JS Helper的返回值,将其动态注入到模板中 [英] Set the return value of Meteor JS Helper, inject this dynamically into template

查看:100
本文介绍了设置Meteor JS Helper的返回值,将其动态注入到模板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:



给出下面的答案,使用似乎是理想的游戏,因为我想在两个不同的模板之间共享数据,并且它应该对发生的任何变化产生响应。

澄清:我的数据来源是 Template.reactiveTable ,目的地是模板.buttons 。当 reactiveTable 中的数据发生变化时,我希望按钮反映这一点。



END编辑



我正在设置<$ c (特别是,< h1> 值)。code>按钮

buttons.html:

 < template name =buttons> 
{{#each testButtons}}
< button id ={{name}}> {{name}}< / button>
{{/每个}}
< / template>

通过这种硬编码,它可以按预期工作(两个按钮呈现为Alice和Bob标签



$ p $ UI.registerHelper('testButtons',function(){
return [
{name:Alice},
{name:Bob}
]
}

模板愉快地接受并呈现这个对象数组。但是,当我动态地生成对象数组时。



目标:对于包含在我的h1标签中的每个唯一字词,生成一个按钮(几乎就像博客标签)。

通过替换我的帮手代码(使用jquery解析< h1> 标签,生成要发送给模板的数组)。

html是:

 < h1> foo< / h1> 
< h1> bar< / h1>
< h1> baz< / h1>

然后在下方运行代码段n控制台,将根据需要创建 uniqStrings = [foo,bar,baz];

  $('h1').map(function(){
var words = [];
words.push($(this).text());
uniqStrings = _.uniq(words);

});

然后我如何将 uniqStrings 发送回我的按钮模板呈现 n = uniqStrings.length 按钮?谢谢!

解决方案

假设这个html:

 <身体GT; 
< h1> Alice< / h1>
< h1> Bob< / h1>
{{>按钮}}
< / body>

< template name =buttons>
{{#each testButtons}}
< button id ={{name}}> {{name}}< / button>
{{/每个}}
< / template>

您可以使用这个实现:

  Template.buttons.helpers({
testButtons:function(){
var words = UI._templateInstance()。state.get('words' );
return _.map(words,function(word){
return {name:word};
});
}
});

Template.buttons.rendered = function(){
var words = $('h1')。map(function(){
return $(this).text( );
});
this.state.set('words',_.uniq(words));
};

Template.buttons.created = function(){
this.state = new ReactiveDict;
};

注意您需要做: meteor add reactive-dict



创建按钮模板时会初始化其内部反应状态。渲染完成后,将使用页面上所有 h1 标签中的文本填充该状态。由于该状态是被动的,因此它会导致 testButtons 运行并将格式正确的数据返回到您的模板。请在作用域反应性上查看我的帖子以获取更多详情。



由于按钮模板使用范围反应性,因此可以在任何页面上随意使用多次,而无需修改父模板。


EDIT:

Given the answers below, using Meteor Deps seems like the ideal play since I want to share data between two distinct templates, and it should be reactive to any changes that occur.

To clarify: source of my data is Template.reactiveTable and the destination is Template.buttons. When the data in reactiveTable changes, I want the buttons to reflect this.

END EDIT

I am looking to set the value of buttons based on the content of a table (specifically, the <h1> values).

buttons.html:

  <template name="buttons">
    {{#each testButtons }}
       <button id="{{ name }}">{{ name }}</button>
    {{/each}}
  </template>

With this hard-coded it works as expected (two buttons render labeled Alice and Bob

UI.registerHelper('testButtons', function () {
        return [ 
          { name: "Alice" }, 
          { name: "Bob" } 
        ]
    }
)

The template happily accepts and renders this array of objects. However, NOT when I generate the array of objects dynamically.

Goal: for each unique word contained within my h1 tags, generate a button (almost like a blog tags).

By replacing my helper code with snippet below (using jquery parsing <h1> tags, generate array to send to template).

Assume html is:

  <h1>foo</h1>
  <h1>bar</h1>
  <h1>baz</h1>

then run snippet below in console, will create uniqStrings = [ "foo", "bar", "baz"]; as desired.

$( 'h1' ).map(function () {
  var words = [];
  words.push($(this).text());
  uniqStrings = _.uniq(words);

});

How do I then send uniqStrings back to my buttons template to render n= uniqStrings.length buttons? Thanks!

解决方案

Assuming this html:

<body>
  <h1>Alice</h1>
  <h1>Bob</h1>
  {{> buttons}}
</body>

<template name="buttons">
  {{#each testButtons }}
    <button id="{{ name }}">{{ name }}</button>
  {{/each}}
</template>

You can get it to work with this implementation:

Template.buttons.helpers({
  testButtons: function() {
    var words = UI._templateInstance().state.get('words');
    return _.map(words, function(word) {
      return {name: word};
    });
  }
});

Template.buttons.rendered = function() {
  var words = $('h1').map(function() {
    return $(this).text();
  });
  this.state.set('words', _.uniq(words));
};

Template.buttons.created = function() {
  this.state = new ReactiveDict;
};

Note you will need to do: meteor add reactive-dict

When the buttons template is created is initializes its own internal reactive state. After it is rendered, it fills that state with the text from all of the h1 tags on the page. Because that state is reactive, it will then cause the testButtons to run and return the correctly formatted data to your template. Please see my post on Scoped Reactivity for more details.

Because the buttons template uses scoped reactivity, it can be used as many times as you like on any page without modification to the parent template.

这篇关于设置Meteor JS Helper的返回值,将其动态注入到模板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆