简体   繁体   English

在jQuery中使用函数handler + parameter调用事件

[英]Call event with function handler + parameter in jQuery

I have the following code that is repeated some times, with only some variation: 我有以下代码重复一次,只有一些变化:

$('#mapContainer').on({
    mouseenter: function () {
        Map.ApplyEvents.Country(this.id);
    },
    click: function () {
        Map.Zoom.State(this.id);
    }
},
'.mapaCountry path');

// this is in another function
$('#mapContainer').on({
    mouseenter: function () {
        Map.ApplyEvents.State(this.id);
    },
    click: function () {
        Map.Zoom.County(this.id);
    }
},
'.mapaState path');

In all events, I have to call an anonymous function just to call 1 function inside. 在所有事件中,我必须调用一个匿名函数来调用内部的1个函数。

Is there a way to squeeze this code assigning handler to desirable functions, but also informing parameter with this.id ? 有没有办法将此代码分配处理程序转换为所需的函数,还可以使用this.id通知参数?

I want to achieve something like this: 我希望实现这样的目标:
(note: I know that it's wrong syntax.) (注意:我知道这是错误的语法。)

$('#mapContainer').on({
    mouseenter: Map.ApplyEvents.Country(this.id),
    click: Map.Zoom.State(this.id)
},
'.mapaCountry path');

$('#mapContainer').on({
    mouseenter: Map.ApplyEvents.State(this.id);
    click: Map.Zoom.County(this.id)
},
'.mapaState path');

IMHO, your first snippet is the most idiomatic way to achieve what you want. 恕我直言,你的第一个片段是实现你想要的最惯用的方式。


If you really have zillions of such bindings, and your Map API consists exclusively of functions taking exactly one single string parameter, you could come up with a custom made binder : 如果真有这样的绑定不计其数,而你的地图API 仅仅包括采取恰好一个唯一的字符串参数的功能,你可以拿出一个定制粘合剂:

$.fn.bindIDlistener = function(handlers, sel) {
    var wrapper  = {}, evt;
    for (evt in handlers) {
        wrapper[evt] = function(){ handlers[evt](this.id); };
    }
    this.on(wrapper, sel);
}

//usage :
$('#mapContainer').bindIDlistener({
    mouseenter: Map.ApplyEvents.Country,
    click: Map.Zoom.State
},
'.mapaCountry path');

$('#mapContainer').bindIDlistener({
    mouseenter: Map.ApplyEvents.State,
    click: Map.Zoom.County
},
'.mapaState path');

but I would still advise to strongly ponder the small benefits (20 chars per event handler) of this approach against the quick limitations you can bump in (add yet another point of failure in the chain, code less readable to external party, difficult to pass a second argument to the handlers, etc...) 但我仍然建议强烈思考这种方法的小优点(每个事件处理程序20个字符),以防止你可以遇到的快速限制(在链中添加另一个故障点,代码对外部方的可读性较差,难以通过处理程序的第二个参数等...)

I know I've been annoyed with this before. 我知道我以前对此感到恼火。 If I remember correctly it's a matter of declaration. 如果我没记错的话,这是一个宣言问题。

I think this doesn't work. 我认为这不起作用。

function myFunction(){}

And this does. 这样做。

var myFunction() = function(){}

Maybe this doesn't solve your exact problem as I don't know know how your functions are declared. 也许这并不能解决您的确切问题,因为我不知道您的函数是如何声明的。

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

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