简体   繁体   中英

How do i Meteor Reset client subscription

i have subscribe and publish like this :

publish.js :

Meteor.publish('ProductWithSkipAndLimit', function(skip,limit){
return Product.find({}, {
        sort: {
            createdAt: 1
        },
        skip: skip,
        limit: limit
    });
});

subscribe.js :

Meteor.subscribe('ProductWithSkipAndLimit',0,10);

and it will return to client 10 products from 0 sort by createdAt. Nah i have an event click like this :

'click' : function(e){
e.preventDefault();
Meteor.subscribe('ProductWithSkipAndLimit',10,10);
}

I want to get 10 more products. okay i get that products, but 10 products not reset. so on client i have 20 products.

how i can reset client subscription? so client only have 10 products every subscribe.

Meteor.subscribe:

Subscribe to a record set. Returns a handle that provides stop() and ready() methods.

You need to take handle of Meteor.subscribe

subscription = Meteor.subscribe('ProductWithSkipAndLimit',10,10);

And in events object :

var subscription;

Template.NAME.events({
    'click' : function(e){
      e.preventDefault();
      subscription && subscription.stop();
      subscription = Meteor.subscribe('ProductWithSkipAndLimit',10,10);
    }
})

I think, in click event you have to set Session variable Session.set('more', true);

On client:

Deps.autorun(function() {
   if(Session.get('more')) {
     Meteor.subscribe('ProductWithSkipAndLimit',10,10);
     Session.set('more', false);
   }
});

Or some logic to set current position in collection (10, 20, etc.)

You asked about subscription resetting, but it looks like there is no need to do it manually in your case.

You can subscribe within Tracker.autorun and pass reactive values as subscription parameters.

Then on each skip / limit session variable change the subscription will be reset automatically.

From Meteor official documentation :

If you call Meteor.subscribe within a reactive computation, for example using Tracker.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped; it's not necessary to call stop on subscriptions made from inside autorun.

Here is working example (METEOR@1.1.0.2):

Items = new Meteor.Collection("items");

if(Meteor.isClient) {
    Tracker.autorun(function() {
        Meteor.subscribe("items", Session.get("skip"), Session.get("limit"));
    });

    Template.main.helpers({
        items: function() {
            return Items.find({});
        }
    });

    Template.main.events({
        'click #next' : function(e){
            e.preventDefault();
            var skip = Session.get("skip");
            Session.set("skip", skip + Session.get("limit"));
        },   
        'click #prev' : function(e){
            e.preventDefault();
            var skip = Session.get("skip");
            Session.set("skip", skip - Session.get("limit"));
        }
    });

    Meteor.startup(function() {
        Session.set("skip", 0);
        Session.set("limit", 10);
    });
}

if(Meteor.isServer) {
    if (Items.find({}).fetch().length < 100) {
        _.times(100, function(n) {
            Items.insert({
                name: String(n),
                createdAt: new Date()
            });
        });
    }

    Meteor.publish("items", function(skip, limit) {        
        return Items.find({}, { limit: limit, skip: skip, sort: { createdAt: 1} });
    });
}

template

<template name="main">
    <header>
        <h1>Items</h1>
        <nav>
            <button id="prev">prev</button>
            <button id="next">next</button>
        </nav>
    </header>
    <ul>
    {{#each items}}
        <li>{{name}}</li>
    {{/each}}
    </ul>
</template>

PS Don't forget to remove "autopublish" package

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM