问题
The struts 2 jQuery plugin has a built in publish
/subscribe
framework.
If you define your own publish and subscribe event (for example on a grid) the subscribed function will be called every time the event is published. For details please see (Struts 2 jQuery Subscribe is called more than once)
To prevent this, there is a isSubscribed
method which can be used.
For a grid as:
<sjg:grid id="gridtable"
onBeforeTopics="before_grid_load" >
The JS will be:
$.subscribe('before_grid_load', function(event, data) {
if ( $('#gridtable').isSubscribed('before_grid_load') ){
return ;
}
//go on with function
}
The problem is that the $('#gridtable').isSubscribed('before_grid_load')
returns false
every time!
回答1:
The function isSubscribed
is applied on the element $('#gridtable')
but subscribed to the $(document)
. I have tested with the last element and it didn't work to me. But tried with the first element and it worked.
Script:
<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>
For grid:
<sjg:grid id="gridtable"
onBeforeTopics="beforeTopic" >
回答2:
Check your lib to your jsp
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
Enable jQuery Grid Plugin in your Head Tag
<sj:head jqueryui="true" jquerytheme="redmond" />
来源:https://stackoverflow.com/questions/23708551/struts-2-jquery-plugin-issubscribe-not-working