Meteor:使用Blaze更新集合后强制重新提交整个模板
我有一个更改DOM的模板,我想在保存到数据库时重新加载该模板。在Blaze之前,Meteor如果在模板中的某个地方有一个反应变量,那么它会重新命名整个模板,但是现在我该怎么做呢 我收集了一系列剪辑,设置在一条铁制路由器路线上:Meteor:使用Blaze更新集合后强制重新提交整个模板,meteor,meteor-blaze,Meteor,Meteor Blaze,我有一个更改DOM的模板,我想在保存到数据库时重新加载该模板。在Blaze之前,Meteor如果在模板中的某个地方有一个反应变量,那么它会重新命名整个模板,但是现在我该怎么做呢 我收集了一系列剪辑,设置在一条铁制路由器路线上: ClipsController = RouteController.extend({ data: function() { clips = Clips.find({}, {sort: {created: 1}}); return {clip
ClipsController = RouteController.extend({
data: function() {
clips = Clips.find({}, {sort: {created: 1}});
return {clips: clips};
}
});
以及剪辑的模板:
iron路由器数据操作是被动默认操作
clips = Clips.find({}, {sort: {created: 1}});
取代
clips = Clips.find({}, {sort: {created: 1}}).fetch();
我不相信Blaze提供了任何重新发布整个模板的方法,因为Blaze的重点是进行细粒度的更新 实现这一点的一种快速而肮脏的方法可能是使用Session、模板助手和{{{self}}块,该块包装整个模板,然后在更新之前将Session key设置为true,在导致{{{self}块中的所有内容重新命名之后将Session key设置为false
Template.clips.noRender = function(){
return Session.get("noRender");
}
Template.clip.events({
'click .edit': function(event, template) {
template.$('.tags li').each(function() {
$(this).text($(this).text().replace(/^#(.*)/, "$1"));
});
},
'click .save': function(event, template) {
var data = {
//...
};
Session.set("noRender", true);
Clips.update({_id: this._id}, data, function(){
Session.set("noRender", false);
});
// How to rerender the template ?
}
});
<template name="clips">
{{#unless noRender}}
{{#each clips}}
{{> clip}}
{{/each}}
{{/unless}}
</template>
Template.clips.noRender=function(){
返回会话。获取(“诺伦德”);
}
Template.clip.events({
“单击.编辑”:函数(事件、模板){
模板。$('.tags li')。每个(函数(){
$(this).text($(this).text().replace(/^#(.*)/,“$1”);
});
},
“单击保存”:函数(事件、模板){
风险值数据={
//...
};
Session.set(“noRender”,true);
update({u id:this.\u id},数据,函数(){
Session.set(“noRender”,false);
});
//如何重新提交模板?
}
});
{{{除非诺伦德}
{{{#每个剪辑}
{{>剪辑}
{{/每个}}
{{/除非}
我认为更好的方法是使用
例如:
Tracker.autorun ->
Tracker.afterFlush ->
# DOM is updated, why don't you do something here?
Blaze提供了一种简单的方法:
var template = Template.instance();
var parentDom = /*where to put your Template*/;
Blaze.remove(template.view);
Blaze.render(Template.clips, parentDom);
它所做的是删除无效的模板,并将新模板作为子模板呈现。
我认为这可能是一个更好的解决方案,也是流星方式
../clips.js
Template.clips.onRendered(function(){
this.autorun(function(){
Template.currentData();
});
});
模板自动运行(runFunc)
您可以从onCreate或onRendered回调使用this.autorun来
反应性地更新DOM或模板实例。你可以用
此回调中的Template.currentData()来访问反应数据
模板实例的上下文
两者似乎工作相同,还是我遗漏了什么?第一个返回一个光标(流星对象),另一个给你一个带结果的数组。我知道这一点,但在这种情况下为什么我要返回数组而不是光标?这很聪明!在我自己的例子中,使用稍微不同的模板进行编辑/查看并动态切换模板成功了,但这是一个好把戏。聪明、糟糕,但非常有用。:)阿达莫萨说了什么。聪明,可怕,但非常有用。
Tracker.autorun ->
Tracker.afterFlush ->
# DOM is updated, why don't you do something here?
var template = Template.instance();
var parentDom = /*where to put your Template*/;
Blaze.remove(template.view);
Blaze.render(Template.clips, parentDom);
../clips.js
Template.clips.onRendered(function(){
this.autorun(function(){
Template.currentData();
});
});