struts2 jQuery插件有一个内置的publish
/subscribe
框架
如果您定义了自己的发布和订阅事件(例如在网格上),则每次发布事件时都将调用订阅函数。详情请参见(Struts 2 jQuery订阅不止一次被调用)
为了防止这种情况,可以使用isSubscribed
方法。
对于grid:
<sjg:grid id="gridtable"
onBeforeTopics="before_grid_load" >
JS将是:
$.subscribe('before_grid_load', function(event, data) {
if ( $('#gridtable').isSubscribed('before_grid_load') ){
return ;
}
//go on with function
}
问题是$('#gridtable').isSubscribed('before_grid_load')
每次返回false
!
函数isSubscribed
应用于元素$('#gridtable')
,但订阅了元素$(document)
。我用最后一个元素进行了测试,它对我不起作用。但我尝试了第一个元素,它成功了。
脚本:
<head>
<link href="<s:url value="/css/template_styles.css"/>" type="text/css" rel="stylesheet">
<sj:head />
<title>jQuery Grid</title>
<script type="text/javascript">
$(document).ready(function(){
console.log("Before subscribe");
$("#gridtable").subscribe("beforeTopic", function(topic, data) {
console.log('Topic: '+data, topic);
if ( $("#gridtable").isSubscribed("beforeTopic") ){
console.log('Subscribed: '+data, topic);
return;
}
//go on with function
console.log('Not subscribed: '+data, topic);
});
console.log("After subscribe");
});
</script>
</head>
网格的:
<sjg:grid id="gridtable"
onBeforeTopics="beforeTopic" >
检查您的lib到jsp
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
在Head标签中启用jQuery Grid Plugin
<sj:head jqueryui="true" jquerytheme="redmond" />