我在 onRendered
block 中遇到 Meteor ReactiveVar
问题。这是我的代码:
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
block 中设置它时,我收到以下控制台错误:Uncaught TypeError:无法读取未定义的属性“set”
。我知道这是关于范围界定的,但我不知道如何解决这个问题。
最佳答案
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);
}
});
});
关于jquery - meteor JS : Setting reactive vars in jQuery function blocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35955823/