比 DOMCategory 更好的处理 dom 的方式?
我正在尝试创建一个可以轻松使用 dom 节点的 DSL。使用 DOMCategory 很好,但会增加“use(DOMCategory)”的噪音。有办法避免这种情况吗?
我尝试将脚本调用包装在“use”调用中,但这似乎在闭包中不起作用。
I'm trying to create a DSL that can easily use a dom node. Using DOMCategory is nice, but adds the noise of 'use(DOMCategory)'. Is there a way to avoid that?
I tried wrapping the script call inside a call to 'use', but this doesn't seem to work in closures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 groovy 的运行时
mixin
功能来永久混合该类别。 Class.mixin 将类别中适用的方法添加到类的元类中。将其应用于作为每个类别方法的第一个参数出现的类和接口,并且类别将可用,而无需将代码包含在use(DOMCategory)
块中。示例:
给出:
You can use groovy's runtime
mixin
feature to permanently mix in the category. Class.mixin adds the applicable methods in the category to the class's metaClass. Apply it to the classes and interfaces that appear as the first parameter to each category method, and the category will be available without enclosing the code in ause(DOMCategory)
block.Example:
Gives: