我在-1
区块中遇到Meteor ReactiveVar
的问题。这是我的代码:
onRendered
我成功获得了Template.posts.onRendered(function() {
// get current value for limit, i.e. how many posts are currently displayed
var limit = this.limit.get();
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
// increase limit by 10 and update it
limit += 10;
this.limit.set(limit);
}
});
});
中测试的限制变量,但在尝试在console.log()
块中设置时,我收到以下控制台错误:$(window).scroll
。我知道这是关于范围的,但我不知道如何解决这个问题。
答案 0 :(得分:1)
this
不是jQuery函数中的模板实例,因为范围已经改变,所以你需要保存引用并发送它。
Template.posts.onRendered(function() {
// get current value for limit, i.e. how many posts are currently displayed
var limit = this.limit.get();
var template = this;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
// increase limit by 10 and update it
limit += 10;
template.limit.set(limit);
}
});
});