Note that there are some explanatory texts on larger screens.

plurals
  1. POPreventing unnecessary rendering in Meteor if a document referenced by two templates in Meteor is updated
    primarykey
    data
    text
    <p>I have a Meteor project where two different templates depend on data from the same document:</p> <pre><code>{ "title": "My Project", "data": "My project data" } </code></pre> <p>One template renders the "title", and another renders the "data".</p> <p>If a user changes the "data" field in the document, I do not want the template that uses the "title" field to re-render. I only want the template that uses "data" to re-render.</p> <p>For example, the title may be in a page header which does not need to be re-rendered if the project's data changes.</p> <p>How can this be achieved?</p> <p>My Meteor templates:</p> <pre><code>&lt;head&gt; &lt;title&gt;experiment&lt;/title&gt; &lt;/head&gt; &lt;body&gt; {{&gt;hello}} &lt;/body&gt; &lt;template name="hello"&gt; &lt;div&gt; My projects. &lt;/div&gt; {{#each projects}} &lt;div&gt; {{&gt;projectTitle}} {{&gt;projectData}} &lt;/div&gt; {{/each}} &lt;/template&gt; &lt;template name="projectTitle"&gt; &lt;div&gt; Project title: {{title}} &lt;/div&gt; &lt;/template&gt; &lt;template name="projectData"&gt; &lt;div&gt; Project data: {{data}} &lt;/div&gt; &lt;/template&gt; </code></pre> <p>My Meteor Javascript:</p> <pre><code>Experiments = new Meteor.Collection("experiments"); if (Meteor.isClient) { Template.hello.projects = function() { return Experiments.find(); } Template.projectTitle.title = function() { return Experiments.find().fetch()[0].title; } Template.projectData.data = function() { return Experiments.find().fetch()[0].data; } Template.projectTitle.rendered = function() { console.log('Rendered projectTitle'); } Template.projectData.rendered = function() { console.log('Rendered projectData'); } } </code></pre> <p>This code will currently output "Rendered projectTitle" and "Rendered projectData" whenever either of the fields in the Project document change, which shows that both templates are being re-rendered.</p> <p><strong>Edit</strong> I don't know if there is a 'correct' way to do this, but one could publish two subscriptions, which return separate parts of the document. This seems counter to the philosophy of Meteor, though.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload