ractivejs / ractive

Next-generation DOM manipulation
http://ractive.js.org
MIT License
5.94k stars 396 forks source link

Dynamic name partials do not behave like static partials #3297

Closed marcalexiei closed 5 years ago

marcalexiei commented 5 years ago

Description:

Partial dynamic rendering doesn't behave in the same way as static. Sometimes dynamic rendered partial ignores passed context using the one where it is rendered.

Background of the issue

In our application we have a table with headings / column that change dynamically based on external factors. On first render everything works correctly but when changing the partial list, which is a computed property, the partials are rendered with the wrong context.

Versions affected:

Platforms affected:

All, tested on Chrome

Reproduction:

JSFiddle

<h1>Tables should have always the same content</h1>

<button on-click="@.toggle('changePartials')">Change partials</button>

<h2>With dynamic partials</h2>

<table>
  <tbody>
  {{#each list}}
    <tr>
      <td>List item is {{.}}</td>

      {{#each ~/partialList}}
        {{>`${.}` ^^/}}
      {{/each}}
    </tr>
  {{/each}}
  </tbody>
</table>

<h2>Static partials</h2>

<table>
  <tbody>
    {{#each list}}
      <tr>
        <td>List item is {{.}}</td>

        {{>foo}}
        {{>bar}}
        {{#if ~/changePartials}}
          {{>another}}
        {{/if}}
      </tr>
    {{/each}}
  </tbody>
</table>
const r = window.r = new Ractive({
  el: '#main',
  template: '#template',
  data: {
    list: [1,2,3],

    changeHeadings: true,
  },

  partials: {
    foo: '<td>I\'m foo and my . is {{.}}</td>',
    bar: '<td>I\'m bar . is {{.}}</td>',
    another: '<td>I\'m another . is {{.}}</td>',
  },

  computed: {
    partialList() {
      const changePartials = this.get('changePartials');

      const tableHeadings = [
        'foo',
        'bar'
      ];

      if (changePartials) {
        tableHeadings.push('another');
      }

      return tableHeadings;
    },
  }
});
evs-chris commented 5 years ago

This should now be fixed on edge. I'll wait on feedback for #3295 and backport to 1.1, 1.2, and 1.3 and publish.

evs-chris commented 5 years ago

When travis gets done churning, 1.1.3, 1.2.2, and 1.3.1 should be out with this fix. 1.3.1 also has Ractive.tick, which is flagged as experimental in the changelog.