【问题标题】:MeteorJS: Setting reactive vars in jQuery function blocksMeteorJS:在 jQuery 功能块中设置反应变量
【发布时间】:2016-03-12 17:27:34
【问题描述】:

我在 onRendered 块中遇到了 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 块中设置它时,我收到以下控制台错误:Uncaught TypeError: Cannot read property 'set' of undefined。我知道这是关于范围界定,但我不知道如何解决这个问题。

【问题讨论】:

    标签: jquery meteor


    【解决方案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);
            }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2021-08-18
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多