MeteorJS:在jQuery功能块中设置反应变量

时间:2016-03-12 09:16:07

标签: jquery meteor

我在-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。我知道这是关于范围的,但我不知道如何解决这个问题。

1 个答案:

答案 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);
        }
    });
});