简体   繁体   English

防止通过arguments时立即调用function

[英]Prevent function from being called immediately when passing arguments

I have the following code, which works我有以下代码,它有效

var settings = trumbowyg.o.plugins.giphycrumbs;
settings = $.extend(true, {},
    defaultOptions,
    trumbowyg.o.plugins.giphycrumbs || {}
);

if(!settings.open_modal) {
    settings.open_modal = function() {
        $(settings.modal_selector).modal('show');
    }
}

trumbowyg.addBtnDef('giphycrumbs', {
    fn: settings.open_modal
});

Where settings.open_modal is executed when a button is pressed.按下按钮时执行settings.open_modal的位置。

However, I want to define this function elsewhere (In the plugins section of trumbowyg instead of inside the plugin itself), but to do that, I need to be able to pass a selector to it.但是,我想在其他地方定义这个 function(在 trumbowyg 的插件部分而不是在插件本身内部),但要做到这一点,我需要能够将选择器传递给它。 I've already done this with a similar close_modal function, but I'm having issues with the open_modal function.我已经使用类似的close_modal function 完成了此操作,但我遇到了open_modal function 的问题。

Here is how I would define the function the way I want to:这是我将如何定义 function 我想要的方式:

plugins: {
    giphycrumbs: {
        ...
        modal_selector: '#giphy_modal',
        close_modal: function(selector) {
            $(selector).modal('hide');
        },
        open_modal: function(selector) {
            $(selector).modal('show');
        }
    },
}

This would mean that my addBtnDef call would be changed to something like:这意味着我的addBtnDef调用将更改为:

trumbowyg.addBtnDef('giphycrumbs', {
    fn: settings.open_modal(settings.modal_selector)
});

However, this causes the function to be ran immediately upon initialization instead of waiting for the button to be pressed as it did before I added the selector to the function.但是,这会导致 function在初始化时立即运行,而不是像在将选择器添加到 function 之前那样等待按钮被按下。

How can I make this code wait for the button to be pressed before running the function, like it did before I added the selector?如何让此代码在运行 function 之前等待按钮被按下,就像在添加选择器之前一样?

This is one way to fix this, though I'd prefer a better solution which doesn't require an extra function to work.这是解决此问题的一种方法,尽管我更喜欢更好的解决方案,它不需要额外的 function 即可工作。

First, declare settings outside of the plugin init, and assign the settings to it the same way as before.首先,在插件初始化之外声明settings ,并以与以前相同的方式为其分配设置。

var settings = null;

...

giphycrumbs: {
    init: function (trumbowyg) {
        settings = trumbowyg.o.plugins.giphycrumbs;
        ...

Next, create a new function which does not use any arguments which just calls settings.open_modal(settings.modal_selector) .接下来,创建一个新的 function ,它不使用任何只调用settings.open_modal(settings.modal_selector)的 arguments 。

function run_open_modal() {
    settings.open_modal(settings.modal_selector);
}

And call that function in the addBtnDef call.并在 addBtnDef 调用中调用 function。

trumbowyg.addBtnDef('giphycrumbs', {
    fn: run_open_modal
});

is open_modal declared beforehand?是否预先声明了 open_modal? Because, if it isn't, you could declare the settings as:因为,如果不是,您可以将设置声明为:

 if(!settings.open_modal) {
 settings.open_modal = function(selector) {
    selector= selector||settings.modal_selector.
      $(selector).modal('show');
   }
} 

So in case you can redefine it, make the orginal closure accept a selector.因此,如果您可以重新定义它,请让原始闭包接受选择器。 If you don't pass one, then it defaults to its initial behavior.如果您不通过,则默认为初始行为。

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

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