在Grails的自定义标记库中包含JQuery资源的正确方法
我正在为grails中的日期时间选择器制作一个自定义标记库。代码如下:在Grails的自定义标记库中包含JQuery资源的正确方法,jquery,grails,taglib,custom-tag,Jquery,Grails,Taglib,Custom Tag,我正在为grails中的日期时间选择器制作一个自定义标记库。代码如下: class jqueryDateTimePickerTagLib { def dateTimePicker = {attrs, body -> def out = out def name = attrs.name //The name attribute is required for the tag to work seamlessly with grails def id = at
class jqueryDateTimePickerTagLib {
def dateTimePicker = {attrs, body ->
def out = out
def name = attrs.name //The name attribute is required for the tag to work seamlessly with grails
def id = attrs.id ?: name
def value = attrs.value ?: new Date().format("yyyy/MM/dd HH:mm")
//Create input data text field for contaning Date Time string
out.println "<input type=\"text\" name=\"${name}\" id=\"${id}\" value=\"${value}\" ><img id=\"${id}opencalender\" src=\"${resource(dir:'images', file:'calendar_btn.png')}\"></input>"
// Adding Resources
out.println "<link rel=\"stylesheet\" href=\"${resource(dir:'css', file:'jquery.datetimepicker.css')}\" />"
out.println "<script src=\"${resource(dir:'js/jquery', file:'jquery.js')}\"></script>"
out.println "<script src=\"${resource(dir:'js/jquery/ui', file:'jquery.datetimepicker.js')}\"></script>"
// java script code
out.println "<script>"
out.println "\$('#${id}').datetimepicker({"
out.println "});"
out.println "\$('#${id}opencalender').click(function(){"
out.println "\$('#${id}').datetimepicker('show');"
out.println "});"
out.println "</script>"
}
}
def resources = { attrs ->
String style = attrs.style ? "${attrs.remove('style')}" : null;
String theme = attrs.theme ? "css/${attrs.remove('theme')}.css" : "css/tiger.css";
String lang = attrs.lang ? "calendar-${attrs.remove('lang')}.js" : "calendar-en.js";
if(style) {
out << "<style type='text/css'>@import url(${style});</style>"
} else {
out << "<style type='text/css'>@import url(${resource(dir:pluginContextPath,file:theme)});</style>"
}
out << """
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/calendar.js")}"></script>\n
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/lang/$lang")}"></script>\n
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/calendar-setup.js")}"></script>\n
"""
}
class jqueryDateTimePickerTagLib{
def dateTimePicker={attrs,body->
def out=out
def name=attrs.name//name属性是标记与grails无缝工作所必需的
def id=attrs.id?:名称
def value=attrs.value?:新日期()格式(“yyyy/MM/dd HH:MM”)
//为连续日期时间字符串创建输入数据文本字段
out.println“”
//增加资源
out.println“”
out.println“”
out.println“”
//java脚本代码
out.println“”
out.println“\$('\${id}')。日期时间选择器({”
out.println“}”)
out.println“\$('\${id}OpenCalendar')。单击(函数(){”
out.println“\$('\${id}')。日期时间选择器('show');”
out.println“}”)
out.println“”
}
}
问题发生在我添加JQuery文件资源的地方。当此自定义日期时间选择器在GSP表单中包含两次时,会导致对每个包含多次声明JQuery资源。
我看到了一些标记库的示例,在这些标记库中,JQuery文件资源以不同的定义添加,如下所示:
class jqueryDateTimePickerTagLib {
def dateTimePicker = {attrs, body ->
def out = out
def name = attrs.name //The name attribute is required for the tag to work seamlessly with grails
def id = attrs.id ?: name
def value = attrs.value ?: new Date().format("yyyy/MM/dd HH:mm")
//Create input data text field for contaning Date Time string
out.println "<input type=\"text\" name=\"${name}\" id=\"${id}\" value=\"${value}\" ><img id=\"${id}opencalender\" src=\"${resource(dir:'images', file:'calendar_btn.png')}\"></input>"
// Adding Resources
out.println "<link rel=\"stylesheet\" href=\"${resource(dir:'css', file:'jquery.datetimepicker.css')}\" />"
out.println "<script src=\"${resource(dir:'js/jquery', file:'jquery.js')}\"></script>"
out.println "<script src=\"${resource(dir:'js/jquery/ui', file:'jquery.datetimepicker.js')}\"></script>"
// java script code
out.println "<script>"
out.println "\$('#${id}').datetimepicker({"
out.println "});"
out.println "\$('#${id}opencalender').click(function(){"
out.println "\$('#${id}').datetimepicker('show');"
out.println "});"
out.println "</script>"
}
}
def resources = { attrs ->
String style = attrs.style ? "${attrs.remove('style')}" : null;
String theme = attrs.theme ? "css/${attrs.remove('theme')}.css" : "css/tiger.css";
String lang = attrs.lang ? "calendar-${attrs.remove('lang')}.js" : "calendar-en.js";
if(style) {
out << "<style type='text/css'>@import url(${style});</style>"
} else {
out << "<style type='text/css'>@import url(${resource(dir:pluginContextPath,file:theme)});</style>"
}
out << """
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/calendar.js")}"></script>\n
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/lang/$lang")}"></script>\n
<script type="text/javascript" src="${resource(dir:pluginContextPath,file:"js/calendar-setup.js")}"></script>\n
"""
}
def资源={attrs->
字符串样式=attrs.style?”${attrs.remove('style')}):null;
字符串theme=attrs.theme?“css/${attrs.remove('theme')}.css”:“css/tiger.css”;
字符串lang=attrs.lang?“calendar-${attrs.remove('lang')}.js”:“calendar en.js”;
如果(样式){
outdef init={
out@moskiteau…即使应用了您的建议,JQuery文件也不会自动包含在我的GSP页面的标题中。