简体   繁体   English

将参数传递给matchmedia addListener回调函数

[英]pass argument to matchmedia addListener callback function

I'm building a plugin and for the toggling certain behaviour on/off, I'm using the matchMedia functions. 我正在构建一个插件,并且为了打开/关闭某些行为,我正在使用matchMedia函数。 However in the callback function, I need to pass an argument to retain the right reference to this. 但是在回调函数中,我需要传递一个参数来保留对它的正确引用。 However, when I try to pass an argument to my callback function, it says my entire addListener callback function is undefined. 但是,当我尝试将参数传递给我的回调函数时,它说我的整个addListener回调函数是未定义的。 The same applies when I try to bind this to the callback function. 当我尝试将其绑定到回调函数时,同样适用。

TypeError: this.mediaQueryCheck(...) is undefined

There must be something really obvious I'm missing about addListener, so for now I'm only including a non-functional example of my code : 关于addListener,我一定有一些非常明显的东西,所以现在我只包含了我的代码的非功能性示例:

     MyPlugin.prototype = {
        version :   '0.0.1',
        mediaQuery : window.matchMedia(defaults.breakpoint),

        mediaQueryCheck : function(mql){

             if(mql.matches === true){ // if our mediaQuery matches
                this.evalScrollPosition();
                if(this.isLaunched === false){
                    // attach scroll handlers

                    $(window).on('scroll resize', this.evalScrollPosition.bind(this));
                    this.isLaunched = true;
                }
            }
            else if(mql.matches === false){ // if the mediaQuery isn't active atm
                if(this.isLaunched === true){
                // remove handlers
                $(window).off('scroll resize', this.evalScrollPosition.bind(this));
                    this.isLaunched = false;
                }
                this.fixedStatus = '';
                this.unstyleContainer(); // remove positioning set by plugin
                this.unstyleColumns(); // remove positioning set by plugin
            }

        },

        init: function(){

            // merge user options with defaults 
            this.config = $.extend({}, defaults, this.options, this.metadata);
            // define mql object

            this.mediaQuery = window.matchMedia(this.config.breakpoint);

             var thatMediaQuery = this.mediaQuery;
            // add listener to conditionally toggle scroll and resize listeners and bind this to not lose reference to this when running the mediaQueryCheck function

            this.mediaQuery.addListener( this.mediaQueryCheck(thatMediaQuery).bind(this) );

            // check mediaQuery to determine whether to apply eventListeners 

            // and run for a first time
            this.mediaQueryCheck(thatMediaQuery);


            return this;
        },
/* .. rest omitted for brevity */
}

So I've also tried passing the reference to this to my mediaQueryCheck function by adding a second parameter to that function and then passing in this like so : 所以我也尝试通过向该函数添加第二个参数然后传递给我的mediaQueryCheck函数来引用它,如下所示:

mediaQueryCheck : function(mql, context){
    // '..'
},
init: function(){
    // 'rest omitted for brevity'
    this.mediaQuery.addListener( this.mediaQueryCheck(thatMediaQuery, this) );
},

but to no avail.. any ideas ? 但无济于事..任何想法? thanks in advance! 提前致谢!

First of all - the second code you provided is not a valid way to bind this. 首先 - 您提供的第二个代码不是绑定它的有效方法。 To bind this you have to use either a bind function (as you did in the first piece of code) or provide something similar on your own. 要绑定它,你必须使用绑定函数(就像你在第一段代码中所做的那样)或者提供类似的东西。

Quick fix, replace: 快速修复,替换:

this.mediaQuery.addListener( this.mediaQueryCheck(thatMediaQuery).bind(this) );

with

this.mediaQuery.addListener(this.mediaQueryCheck.bind(this));

You don't need to invoke the function when you try to bind "this". 当您尝试绑定“this”时,不需要调用该函数。 If it doesn't work, please paste more of your code (the whole function would be awesome). 如果它不起作用,请粘贴更多代码(整个功能都很棒)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM