Jid*_*ide 7 meteor meteor-blaze
我有一个模板,其中DOM被更改,我想在保存到数据库时重新呈现模板.在Blaze之前,如果模板中某处存在反应变量,Meteor会重新渲染整个模板,但现在我该怎么做?
我有一个在Iron路由器路由中设置的剪辑集合:
ClipsController = RouteController.extend({
data: function() {
clips = Clips.find({}, {sort: {created: 1}});
return {clips: clips};
}
});
Run Code Online (Sandbox Code Playgroud)
和剪辑的模板:
<template name="clips">
{{#each clips}}
{{> clip}}
{{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)
然后,我有一个剪辑模板:
<template name="clip">
<article class="clip" id="{{_id}}">
{{{content}}}
<ul class="tags">
{{#each tags}}
<li><a href="/#{{this}}">#{{this}}</a></li>
{{/each}}
</ul>
</article>
</template>
Run Code Online (Sandbox Code Playgroud)
此模板的脚本更改DOM然后保存剪辑:
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 = {
//...
};
Clips.update({_id: this._id}, data);
// How to rerender the template ?
}
});
Run Code Online (Sandbox Code Playgroud)
Kel*_*ley 13
我不相信Blaze提供任何方式来重新渲染整个模板,因为Blaze的目的是进行细粒度的更新.
实现这一目标的快速而简单的方法可能是使用Session,模板帮助器和包装整个模板的{{#unless}}块,然后在更新之前将Session键设置为true,并在导致所有内容之后将false设置为false要重新呈现的{{#unless}}块.
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9763 次 |
| 最近记录: |