【发布时间】:2016-10-22 01:09:02
【问题描述】:
我正在创建一个带有标签库的小部件,这取决于将大量 JavaScript 推送到浏览器。我将它全部作为文本写在 taglib 中,并用 out
我正在尝试做的简化示例 - 让我们说一个显示用户名的“名称”标签,但是当您单击它时会提醒用户 ID:
作为我可以写的标签:
class FooTagLib {
static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
static namespace = 'foo'
def name = {attrs, body ->
out << "<p id = ${attrs.uid}>${attrs.username}</p>"
out << "<script type='text/javascript'>"
out << """
\$('#{$attrs.uid}.click( function() {
alert("ID for user ${attrs.username} is ${attrs.uid}); });
"""
}
}
然后在我的 gsp 中我可以写:a
<foo:name username="${user.name}" uid="${user.id}"></foo:name>
我想不通的是如何从 taglib 中提取 click 函数到一个 javascript 库中,所以我可以将 FooTagLib 变成这样:How do I pass attrs.username and attrs.uid so it is accessible in javascript文件?
class FooTagLib {
static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
static namespace = 'foo'
def name = {attrs, body ->
out << "<p id = ${attrs.uid}>${attrs.username}</p>"
out << "<asset: type='text/javascript'>"
out << "<asset:javascript src='footag.js'/>
}
}
【问题讨论】: