如何扩展本机 mootools 方法
是否可以扩展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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然这并非不可能,但这是一种值得怀疑的做法——更改 mootools 内部 api。除非您精通 mootools 并遵循 github 上的开发方向,并且知道您的更改不会破坏未来的兼容性,否则我建议您不要这样做。
在我看来,您有两条路线:
通过
implement
创建一个新的 Element 方法来执行您的逻辑。例如:Element.addMyEvent
执行您的操作,然后调用正常的element.addEvent
。这是更好的选择,并且没有真正的不利影响(见上文)直接更改原型。意味着您无需重构任何代码,它就会正常工作。这可能意味着使用您的代码的其他人将难以遵循您的代码,并且难以进行跟踪/故障排除 - 想一想,了解 mootools 和标准
addEvent
行为的人甚至不会想到检查如果出现问题,请直接更改原型。如果 mootools 放弃
Element.prototype
修改而转而采用包装器(为了与其他框架兼容),那么即将推出的 mootools 2.0 可能会使上面的方法 2 无效。返回到方法 1 :)我认为解决方案 1 更好、更明显。
至于2: http://jsfiddle.net/dimitar/aTukP/
就是这么简单。请记住
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:
make a new Element method via
implement
that does your logic. eg:Element.addMyEvent
that does your thing, then calls the normalelement.addEvent
after. this is preferable and has no real adverse effects (see above)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.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/
it is that simple. keep in mind
Element.events
also should go to document and window. also, this won't change theEvents
class mixin, for that you need to refactorEvents.addEvent
instead.