jquery - meteor JS : Setting reactive vars in jQuery function blocks

标签 jquery meteor

我在 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/

相关文章:

javascript - Coffeescript 在页面上打印 HTML,而不是渲染它

javascript - 订阅 Meteor.users 集合更新

javascript - 如何获取父类最接近的表单元素。 jQuery

jquery - 使用 jquery 悬停更改显示属性

javascript - 在 Meteor.js 中显示其他用户的电子邮件地址

javascript - 更新到 0.7.1.1 后 meteor 中断无法调用未定义的方法 'split'

javascript - 如何在 Meteorjs 中提交隐藏输入的值

javascript - 使用 chrome 扩展获取 DNS 错误和错误 404

javascript - 类型错误 : object is not a function - jquery

jquery - 如何在 Twitter Bootstrap 工具提示上启用淡入淡出效果?