Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何重置Meteor客户端订阅_Javascript_Meteor - Fatal编程技术网

Javascript 如何重置Meteor客户端订阅

Javascript 如何重置Meteor客户端订阅,javascript,meteor,Javascript,Meteor,我已订阅并发布如下内容: 'click' : function(e){ e.preventDefault(); Meteor.subscribe('ProductWithSkipAndLimit',10,10); } publish.js: Meteor.publish('ProductWithSkipAndLimit', function(skip,limit){ return Product.find({}, { sort: { createdAt:

我已订阅并发布如下内容:

'click' : function(e){
e.preventDefault();
Meteor.subscribe('ProductWithSkipAndLimit',10,10);
}
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);
它将从0按createdAt排序返回到客户端10个产品。 不,我有一个事件点击如下:

'click' : function(e){
e.preventDefault();
Meteor.subscribe('ProductWithSkipAndLimit',10,10);
}
我想再买10种产品。好的,我得到产品,但10个产品没有重置。所以我有20种产品

如何重置客户端订阅?因此,客户每次订阅只有10种产品。

Meteor.subscribe:

订阅一个记录集。返回提供stop()和ready()方法的句柄

你需要控制流星号

subscription = Meteor.subscribe('ProductWithSkipAndLimit',10,10);
在事件对象中:

var subscription;

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

我认为,在
点击
事件中,您必须设置会话变量
Session.set('more',true)

在客户端:

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

或者使用一些逻辑来设置集合中的当前位置(10、20等)。

您询问了订阅重置,但在您的情况下,似乎不需要手动执行

您可以在
Tracker.autorun
中订阅,并将响应值作为订阅参数传递

然后,在每次
跳过
/
限制
会话变量更改时,订阅将自动重置

发件人:

如果在反应式计算中调用Meteor.subscribe,例如使用Tracker.autorun,当计算无效或停止时,订阅将自动取消;对于从自动运行内部进行的订阅,不必调用stop

下面是一个工作示例(METEOR@1.1.0.2):

Items=新流星系列(“Items”);
if(Meteor.isClient){
Tracker.autorun(函数(){
Meteor.subscribe(“items”)、Session.get(“skip”)、Session.get(“limit”);
});
Template.main.helpers({
项目:功能(){
返回项。查找({});
}
});
Template.main.events({
“单击#下一步”:函数(e){
e、 预防默认值();
var skip=Session.get(“skip”);
Session.set(“跳过”,跳过+Session.get(“限制”);
},   
“单击#上一步”:函数(e){
e、 预防默认值();
var skip=Session.get(“skip”);
Session.set(“skip”,skip-Session.get(“limit”);
}
});
Meteor.startup(函数(){
Session.set(“跳过”,0);
设置(“限制”,第10节);
});
}
if(Meteor.isServer){
if(Items.find({}).fetch().length<100){
_.次(100,功能(n){
项目.插入({
名称:字符串(n),
createdAt:新日期()
});
});
}
Meteor.publish(“项目”),函数(跳过,限制){
返回项。查找({},{limit:limit,skip:skip,sort:{createdAt:1}});
});
}
模板

<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>

项目
上
下一个
    {{{#每项}
  • {{name}}
  • {{/每个}}

注意:别忘了删除“自动发布”软件包

Hello kuba,但我将订阅放在ironrouter waiton上,所以我相信方法是通过会话将ironrouter waiton的订阅传递给模板内的用户。然后在第一次单击分页时,开始使用Template.events中的Subscription,如上所述。让我知道这是否有效。如果能知道为什么答案被否决,那就太好了。