0

我有一段代码,这段代码在 Backbone 应用程序中更改路由时执行:

mediaMatchUnRegister: function () {
            if (this.mediaMatchState.registered) {
                enquire.unregister(this.mediaMatchState.tabletAndUpQuery)
                        .unregister(this.mediaMatchState.mobileQuery);
                this.mediaMatchState.registered = false;
            }
        }

这一切都运行得很好,除了以下条件:

  1. 应用程序正在 Chrome 中运行
  2. 我点击了该页面上的“打印”按钮,这会打开 Chrome 打印预览对话框。

如果我没有调出 Chrome 打印预览对话框,那么它运行得很好。FF 和 IE,没有这个对话框,对此没有任何问题。

问题在于enquire.js,特别是在clear()方法中:

clear : function() {
                each(this.handlers, function(handler) {
                    handler.destroy();
                });
                this.mql.removeListener(this.listener);
                this.handlers.length = 0; //clear array
            },

如果我没有触发打印预览对话框this.mqlMediaQueryList. 如果我触发了它,那么this.mql现在是MediaQueryListEvent. 然后它失败了,因为这个对象似乎没有 removeListener 方法。

有人遇到过这种情况么?Chrome 打印预览对话框是红鲱鱼吗?

4

1 回答 1

0

我有同样的问题。我找不到合理的解释,所以我只是在调用之前进行了检查removeListener()

if(this.mql.removeListener)

它远非理想,但至少可以阻止代码破坏。

于 2015-03-23T13:02:42.250 回答