如何扩展本机 mootools 方法

发布于 2024-11-25 06:55:54 字数 322 浏览 2 评论 0原文

是否可以扩展mootools中的addEvent函数来执行某些操作,并调用普通的addEvent方法?或者,如果有人有更好的方法来做我需要的事情,我会等很多年。

根据我在网站上的页面,我有不同的“点击”处理程序。此外,每一页上可能有多个。除了执行单击侦听器将执行的操作之外,我还希望页面上的每次单击都执行一段代码。在每个处理程序上添加这两行,至少可以说是 PITA,所以我考虑重写 addEvent ,每次我添加“点击”侦听器时,它都会创建一个执行的新函数代码,然后调用函数。

知道我该怎么做吗?

Is it possible to extend the addEvent function in mootools to do something and also calls the normal addEvent method? Or if someone has a better way to do what I need I'm all years.

I have different 'click' handlers depending on which page I'm on the site. Also, there might be more than one on each page. I want to have every click on the page execute a piece of code, besides doing whatever that click listener will do. Adding that two lines on each of the handlers, would be a PITA to say the least, so I thought about overriding the addEvent that every time I add a 'click' listener it will create a new function executing the code and then calling the function.

Any idea how I could do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-12-02 06:55:54

虽然这并非不可能,但这是一种值得怀疑的做法——更改 mootools 内部 api。除非您精通 mootools 并遵循 github 上的开发方向,并且知道您的更改不会破坏未来的兼容性,否则我建议您不要这样做。

在我看来,您有两条路线:

  1. 通过 implement 创建一个新的 Element 方法来执行您的逻辑。例如: Element.addMyEvent 执行您的操作,然后调用正常的 element.addEvent 。这是更好的选择,并且没有真正的不利影响(见上文)

  2. 直接更改原型。意味着您无需重构任何代码,它就会正常工作。这可能意味着使用您的代码的其他人将难以遵循您的代码,并且难以进行跟踪/故障排除 - 想一想,了解 mootools 和标准 addEvent 行为的人甚至不会想到检查如果出现问题,请直接更改原型。

  3. 如果 mootools 放弃 Element.prototype 修改而转而采用包装器(为了与其他框架兼容),那么即将推出的 mootools 2.0 可能会使上面的方法 2 无效。返回到方法 1 :)

我认为解决方案 1 更好、更明显。

至于2: http://jsfiddle.net/dimitar/aTukP/

(function() {
    // setup a proxy via the Element prototype.
    var oldProto = Element.prototype.addEvent;

    // you really need [Element, Document, Window] but this is fine.
    Element.prototype.addEvent = function(type, fn, internal){
        console.log("added " + type, this); // add new logic here. 'this' == element.
        oldProto.apply(this, arguments);
    };

})();

document.id("foo").addEvent("click", function(e) {
    e.stop();
    console.log("clicked");
    console.log(e);
});

就是这么简单。请记住 Element.events 也应该转到文档和窗口。另外,这不会改变 Events 类 mixin,因为您需要重构 Events.addEvent

Whereas this is not impossible, it's a questionable practice--changing mootools internal apis. Unless you are well versed with mootools and follow dev direction on github and know your change won't break future compatibility, I would recommend against it.

The way I see it, you have two routes:

  1. make a new Element method via implement that does your logic. eg: Element.addMyEvent that does your thing, then calls the normal element.addEvent after. this is preferable and has no real adverse effects (see above)

  2. change the prototype directly. means you don't get to refactor any code and it will just work. this can mean others that get to work with your code will have difficulties following it as well as difficulties tracing/troubleshooting- think, somebody who knows mootools and the standard addEvent behaviour won't even think to check the prototypes if they get problems.

  3. mootools 2.0 coming will likely INVALIDATE method 2 above if mootools moves away from Element.prototype modification in favour of a wrapper (for compatibility with other frameworks). Go back to method 1 :)

I think solution 1 is better and obvious.

as for 2: http://jsfiddle.net/dimitar/aTukP/

(function() {
    // setup a proxy via the Element prototype.
    var oldProto = Element.prototype.addEvent;

    // you really need [Element, Document, Window] but this is fine.
    Element.prototype.addEvent = function(type, fn, internal){
        console.log("added " + type, this); // add new logic here. 'this' == element.
        oldProto.apply(this, arguments);
    };

})();

document.id("foo").addEvent("click", function(e) {
    e.stop();
    console.log("clicked");
    console.log(e);
});

it is that simple. keep in mind Element.events also should go to document and window. also, this won't change the Events class mixin, for that you need to refactor Events.addEvent instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文