1

I have a Meteor template that uses a dynamic subscription:

    var templateId = event.target.value;
    Meteor.subscribe('orderTemplateShow', templateId)

templateId changes depending on the select value I choose:

    <p><label>Select Template</label></p>
    <select id="templateSelect" name="templateSelect">
        <option disabled selected> Select Template </option>
        {{#each orderTemplates}}
            <option value="{{this._id}}">{{this.templateName}}</option>
        {{/each}}
    </select>

Once I select a template, the template's information renders on a table that I have on the template.

My table:

 <table id="templateItems" class="table">
            <thead>
                <tr>
                    <th>Product Code</th>
                    <th>Brand</th>
                    <th>Description</th>
                    <th>Member Price</th>
                    <th>Quantity</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {{#each templateItems}}
                <tr>
                    <td>{{productCode}}</td>
                    <td>{{brand}}</td>
                    <td>{{description}}</td>
                    <td>${{memPrice}}</td>
                    <td><input type="text" id="qty" value ="{{quantity}}"></td>
                    <td><button class="btn btn-primary removeCartItem">Remove</button></td>
                </tr>
                {{/each}}
            </tbody>
        </table>
        </form>

However, when I click on a new template, data from the old template still appears on the table, in addition to data from the new template that I select. Therefore, is there a way for me to dynamically remove data from an old subscription?

Thanks!

j08691
  • 204,283
  • 31
  • 260
  • 272
Trung Tran
  • 13,141
  • 42
  • 113
  • 200
  • 1
    You need an autorun and a template subscription. See my answer to your [previous question](https://stackoverflow.com/questions/32195460/how-can-i-make-meteor-subscriptions-dynamic). – David Weldon Aug 25 '15 at 17:33

1 Answers1

2

When you subscribe to the publication, keep a subscription handle. Then when you want to cancel that subscription, invoke .stop() to cancel it.

var subHandle = Meteor.subscribe('orderTemplateShow', templateId);
...
subHandle.stop()
Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
  • Floyd, this works great. However, I want the subscription to stop starting with the second dropdown selection and for every selection after that. Otherwise, the subscription will stop every time I select a new template. I'll figure out something. Thank you!! – Trung Tran Aug 25 '15 at 16:18
  • have a look at the template events http://meteor.github.io/blaze/docs.html#template_destroyed – Micha Roon Aug 26 '15 at 08:05