针对javascript–MeteorBlaze在Template.onCreated中访问Template.contentBlock和templates下的html怎么访问这两个问题,本篇文章进行了
针对javascript – Meteor Blaze在Template.onCreated中访问Template.contentBlock和templates下的html怎么访问这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展javascript – Bootstrap Popover Meteor JS点击事件、javascript – Meteor / Mongo:查找和更新集合中的某些元素、javascript – Meteor 0.8 Blaze如何更新Jquery插件的渲染更改、javascript – Meteor 0.8.0:构建应用程序时:意外关闭模板标记等相关知识,希望可以帮助到你。
本文目录一览:- javascript – Meteor Blaze在Template.onCreated中访问Template.contentBlock(templates下的html怎么访问)
- javascript – Bootstrap Popover Meteor JS点击事件
- javascript – Meteor / Mongo:查找和更新集合中的某些元素
- javascript – Meteor 0.8 Blaze如何更新Jquery插件的渲染更改
- javascript – Meteor 0.8.0:构建应用程序时:意外关闭模板标记
javascript – Meteor Blaze在Template.onCreated中访问Template.contentBlock(templates下的html怎么访问)
<template name="parent"> {{> Template.contentBlock ..}} </template> <template name="child"> {{> Template.contentBlock ..}} </template>
我的预期用例是拥有一个带有任意子节点的模板,我在html文件中定义.
{{#parent}} {{#child id="child1" title="Child 1"}} <p>This is content of child 1</p> {{/child}} {{#child id="child2" title="Child 2"}} <p>This is content of child 2</p> {{/child}} {{#child id="childN" title="Child N"}} <p>This is content of child N</p> {{/child}} {{/parent}}
到目前为止没问题.但是,在父模板的onCreated / autorun中,我希望能够访问子模板.我想使用这些数据在父模板元素中动态创建
Template.parent.onCreated(function () { const instance = this; instance.state = new ReactiveDict(); instance.autorun(function () { const contentBlocks = // how? instance.state.set("children",contentBlocks); }); }); Template.parent.helpers({ children() { return Template.instance().state.get("children"); } });
子项将在父模板中使用如下:
{{#parent}} {{#each children}} do something with {{this.value}} {{/each}} {{#child id="child1" title="Child 1"}} <p>This is content of child 1</p> {{/child}} {{#child id="child2" title="Child 2"}} <p>This is content of child 2</p> {{/child}} {{#child id="childN" title="Child N"}} <p>This is content of child N</p> {{/child}} {{/parent}}
我不想要的是访问contentBlock的内容(< p>),而是获取添加的子模板的列表.
这可能与当前的Template / Blaze API有关吗? documentation在这一点上有点薄.
这基本上与这篇文章相反:How to get the parent template instance (of the current template)
编辑1:使用父视图的渲染功能(仅部分工作)
我找到了一种方法来获取父模板的子项,但不是他们的数据反应性:
// in Template.parant.onCreated -> autorun const children = instance.view.templateContentBlock.renderFunction() .filter(child => typeof child === 'object') .map(el => Blaze.getData(el._render())); console.log(children); // null,null,null because Blaze.getData(view) does return null
我找到的另一种方法是使用共享的ReactiveVar,但在我看来都不够干净.我只想在父js代码中获取Template实例列表.
编辑2:使用共享的ReactiveVar(仅部分工作)
只要它在两个模板的范围内,就可以使用共享的ReactiveVar:
const _cache = new ReactiveVar({}); Template.parent.onCreated(function () { const instance = this; instance.state = new ReactiveDict(); instance.autorun(function () { const children = Object.values(_cache.get()); instance.state.set("children",children); }); }); Template.parent.helpers({ children() { return Template.instance().state.get("children"); } });
工作(但只渲染一次,不反应):
Template.child.onCreated(function () { const instance = this; const data = Template.currentData(); const cache = _cache.get(); cache[data.id] = data; _cache.set(cache); });
不工作(子自动运行是设置值,但不呈现新值):
Template.child.onCreated(function () { const instance = this; instance.autorun(function() { const instance = this; const data = Template.currentData(); const cache = _cache.get(); cache[data.id] = data; _cache.set(cache); }); });
解决方法
main.html中:
<body> {{> content}} </body> <template name="content"> {{#parent}} {{#each children}} <p>do something with {{this.id}}</p> <p>data: {{this.tmpl.data.title}}</p> {{/each}} {{#child id="child1" title="Child 1" parentTemplate=this.parentTemplate}} <p>This is content of child 1</p> {{/child}} {{#child id="child2" title="Child 2" parentTemplate=this.parentTemplate }} <p>This is content of child 2</p> {{/child}} {{#child id="childN" title="Child N" parentTemplate=this.parentTemplate }} <p>This is content of child N</p> {{/child}} {{/parent}} </template> <template name="parent"> {{> Template.contentBlock parentTemplate=template}} </template> <template name="child"> {{> Template.contentBlock }} </template>
main.js
import { Template } from 'meteor/templating'; import { ReactiveVar } from 'meteor/reactive-var'; import './main.html'; Template.content.helpers({ children() { return this.parentTemplate.children.get(); },}); Template.parent.onCreated(function () { this.children = new ReactiveVar([]); }); Template.parent.helpers({ template() { return Template.instance(); } }); Template.child.onRendered(function () { const children = this.data.parentTemplate.children.get(); children.push({ id: this.data.id,tmpl: this }); this.data.parentTemplate.children.set(children); });
输出:
虽然它使用的ReactiveVar并不理想,它不依赖于任何全局,你可以将你的代码放在不同的文件中,没问题.
javascript – Bootstrap Popover Meteor JS点击事件
这是我的代码:
<button name="newLetter"type="button" data-container="body" data-toggle="popover" data-placement="right"></button> <div id="popover-content"> <textarea></textarea> <button></button> </div>
Template.templateName.events({ 'click .newLetter': function(e,t) { $(".newLetter").popover({ html: true,title: 'New Letter',content: function() { return $("#popover-content").html(); } }); },'click .addNewLetter': function(e,t) { console.log('test'); } });
任何帮助将不胜感激.
解决方法
Template.templateName.rendered = function() { $(".newLetter").popover({ html: true,content: function() { return $("#popover-content").html(); } }); }
如果您使用调试器进行检查,您将看到每次单击.newLetter按钮时,bootstrap会获取#popover-content的内容并将其放在带有.popover类的新div中.如果您想了解如何在动态创建的元素上绑定事件,you should check this answer.(解决方案是使用on()
)
现在,对于正在发生的事情,Meteor正在#popover-content内部的.addNewLetter上绑定一个click事件,而不是在div.popover中动态创建的元素.addNewLetter上绑定,这就是它无法正常工作的原因.我找到了一个解决方法:
Template.templateName.rendered = function() { $(document).on('click','.addNewLetter',function() { console.log('hey'); }); }
javascript – Meteor / Mongo:查找和更新集合中的某些元素
对于特征A =真和B =真的每个条目,将特征C更改为假.
到目前为止,我一直在试图找出Mongo如何处理集合元素中的“for each”循环,并且对于每个元素检查条件A和B是否成立,然后是collection.update(element,{C :false}).这证明比我想象的问题更多.我想做这样的事情(使用虚拟变量名):
for (i = 0; i < collection.find().count(); i++){ if (collection[i].A===true && collection[i].B===true) collection.update(collection[i],{$set: {C: false}}); };
我一直在改变这个基础代码,但是开始感觉到我缺少一些关于索引的基本知识/如何在Mongo中收集工作.你可以索引这样的集合(如果是这样,这甚至是最方便的方式来做我想做的事情)?
解决方法
当然,这是一个简单的解决方案:
collection.update({A: true,B: true},{$set: {C:false}});
javascript – Meteor 0.8 Blaze如何更新Jquery插件的渲染更改
我的问题是如何在DOM中更新一组元素时获得1个事件或渲染回调?如果我按照Blaze wiki https://github.com/avital/meteor-ui-new-rendered-callback中的链接,这不是我想要的.如果我遵循第二个建议,我将获得尽可能多的渲染调用,因为我有元素.并且父元素只会在页面加载时获得1个渲染回调.
在我的例子中,我使用Footable Jquery插件来格式化表格.初始加载工作正常,但是如果我在Collection find中更改过滤器变量,则DOM会更新并且不会再次调用渲染,因为Blaze只调用一次渲染.我不想把它放到另一个模板中,因为这只意味着对渲染的多次调用,因此当它只需要一个整个表时,就会多次调用Footable.
任何帮助表示赞赏.
<template name="customerData">
<table>
{{#each daTarows}}
<tr>
<td>{{first_name}}</td>
<td>{{last_name}}</td>
<td>{{email}}</td>
{{#each phones}}
<td>{{phone}}</td>
{{/each}}
</tr>
{{/each}}
</table>
</template>
Template.customerData.rendered = function(){
$(".table").footable();
}
Template.customerData.phones = function(){
var result = [];
_.each(this.phoneNumbers, function(element, index, list){
result.push({ phone: element});
});
return result;
}
解决方法:
最好的解决方案是编写自定义块帮助程序. Lemme为你做:)
履行
UI.registerHelper('footableBody', function () {
var dependency = new Deps.Dependency(),
dataSource = this,
handle, footable;
return UI.Component.extend({
render: function () {
var self = this;
return UI.Each(function () {
return dataSource;
}, UI.block(function () {
dependency.changed();
return self.__content;
}));
},
rendered: function () {
var $node = $(self.firstNode).closest('table');
handle = Deps.autorun(function () {
if (!footable) {
$node.footable();
footable = $node.data('footable');
} else {
footable.redraw();
}
dependency.depend();
});
},
destroyed: function () {
handle && handle.stop();
},
});
});
用法
现在,在您的模板中,您可以执行以下操作:
<table>
<thead>
...
</thead>
<tbody>
{{#footableBody daTarows}}
<tr>
<td>{{first_name}}</td>
<td>{{last_name}}</td>
<td>{{email}}</td>
<td>{{phone}}</td>
</tr>
{{/footableBody}}
</tbody>
</table>
当然,您应该根据自己的需要自定义帮助程序的行为.
思考
还有另一种 – 更通用的 – 解决方案遵循标记帮助程序如何实现here的模式.但是,我不鼓励将此策略应用于您的特定用例.
javascript – Meteor 0.8.0:构建应用程序时:意外关闭模板标记
{{#if selected}} <div> {{else}} <div> {{/if}} <i></i> {{title}} </div>
这段代码有什么问题?
解决方法
<div> <i></i> {{title}} </div>
只要你没有用你的助手破坏标签开始/结束,你应该没问题.
史蒂夫
今天关于javascript – Meteor Blaze在Template.onCreated中访问Template.contentBlock和templates下的html怎么访问的讲解已经结束,谢谢您的阅读,如果想了解更多关于javascript – Bootstrap Popover Meteor JS点击事件、javascript – Meteor / Mongo:查找和更新集合中的某些元素、javascript – Meteor 0.8 Blaze如何更新Jquery插件的渲染更改、javascript – Meteor 0.8.0:构建应用程序时:意外关闭模板标记的相关知识,请在本站搜索。
本文标签: