Struts 2 jQuery plugin isSubscribe not working

ε祈祈猫儿з 提交于 2019-12-09 23:32:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!