【问题标题】:Can’t call View#subscribe from inside the destroyed callback无法从销毁的回调中调用 View#subscribe
【发布时间】:2016-12-25 10:48:34
【问题描述】:

我在 template.onDestoyed 中遇到了一个错误和奇怪的行为;

我有无限滚动订阅的代码(它存储在特殊的订阅模板中)它工作正常,直到我切换到另一条路线,并创建一个订阅模板的新实例。

代码:

  Template.subscriber.onCreated(function() {
        var template = this;
        var skipCount = 0;
        template.autorun(function(c) {
            template.subscribe(template.data.name, skipCount, template.data.user);
            var block = true;
            $(window).scroll(function() {
                if (($(window).scrollTop() + $(window).height()) >= ($(document).height()) && block) {
                    block = false;
                    skipCount = skipCount + template.data.count;
                    console.log(template.data);
                    console.log("skip_count is "+skipCount);
                    template.subscribe(template.data.name, skipCount, template.data.user, {
                        onReady: function() {
                            block = true;
                        },
                        onStop: function() {
                            console.log('route switched, subscribtion stopped');
                        }
                    });
                }
            });
        })
    });

当我在一个页面上“向下滚动”时,订阅者工作正常,当我进入另一个页面并首先“向下滚动”时,我第一次从旧订阅者模板(理论上必须销毁)获取数据。第二次(再次向下滚动)订阅者的新实例开始正常工作。

PIRNT SCREEN CONSOLE

我做错了什么?

【问题讨论】:

    标签: meteor meteor-blaze flow-router


    【解决方案1】:

    哎呀! 流星论坛的好人帮助了我。 实际上问题出在jquery.scroll 事件中。模板被销毁时它没有被清理。 (这是一个错误?还是正常行为?)。我只需要在onDestroyed 部分取消绑定滚动事件。

    【讨论】:

      猜你喜欢
      • 2011-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多