我有一個模板,其中DOM已更改,並且我希望在保存到數據庫時重新渲染模板。在Blaze之前,如果模板中某處存在反應變量,流星會重新渲染整個模板,但現在我怎麼能做到這一點?流星:強制使用Blaze收集更新後重新渲染整個模板
我有剪輯在鐵路由器的路由設置的集合:
ClipsController = RouteController.extend({
data: function() {
clips = Clips.find({}, {sort: {created: 1}});
return {clips: clips};
}
});
和模板爲剪輯:
<template name="clips">
{{#each clips}}
{{> clip}}
{{/each}}
</template>
然後,我有模板clip:
<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>
而對於這個模板,改變了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 ?
}
});
這很聰明!在我自己的情況下,使用略微不同的模板進行編輯/查看並動態切換模板的確有竅門,但這是一個很好的竅門。 – Jide
聰明,可怕,但非常有用。 :) –