gigaZhang / struts2-jquery

Automatically exported from code.google.com/p/struts2-jquery
0 stars 0 forks source link

subscribed to grid events (and other components) is called more than once #1101

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Consider this sample....

One can change the grid behavior when grid is loaded by subscribing too 
onGridCompleteTopics event

<sjg:grid id="gridtable" dataType="json"
    href="%{url}" gridModel="gridModel" direction="%{pageDir}" width="800"
    shrinkToFit="true" onGridCompleteTopics="grid_compelete">

And then in the js:

    $.subscribe('grid_compelete', function(event, data) {
    //do some thing
}

The problem is that, as above js and the grid are in the same page (they are in 
one jsp), every time I reload the page the subscribe is called and the code in 
the subscribe runs again.

How can I prevent it?! I found a function in jquery.subscribe.1.2.3 which is 
called isSubscribed I thought the framework should use it internally to avoid 
this issue. But it is not!

I asked above and suggested to use a cookie to find if the subscription is done 
once so avoid rest of the code.
http://stackoverflow.com/questions/20441178/struts-2-jquery-subscribe-is-called-
more-than-once

Original issue reported on code.google.com by afattah...@gmail.com on 23 Apr 2014 at 11:23

GoogleCodeExporter commented 9 years ago
The topic "grid_compelete" is defined by your self. You can simply define it 
globally or check if it is already subscribed. 

Original comment by johgep on 28 Apr 2014 at 12:50

GoogleCodeExporter commented 9 years ago
Yes I have defined it my self, How can I find if it is already subscribed?! is 
there any isSubscribed available?!

Original comment by afattah...@gmail.com on 10 May 2014 at 5:39

GoogleCodeExporter commented 9 years ago
See line 242:
https://code.google.com/p/struts2-jquery/source/browse/trunk/struts2-jquery-plug
in/src/main/resources/template/js/plugins/jquery.subscribe.js

Another way is to destroy this topic and recreate it. In case of the topic is 
differnet then the already existing.

Original comment by johgep on 13 May 2014 at 6:40

GoogleCodeExporter commented 9 years ago
Thanks I tried below:

    <sjg:grid id="gridtable" 
            onBeforeTopics="before_grid_load" >

And

 $.subscribe('before_grid_load', function(event, data) {        

            if ( $('#gridtable').isSubscribed('before_grid_load') ){
             return ;    }
    //go on with function
    }

but $('#gridtable').isSubscribed('before_grid_load') always return false!

Original comment by afattah...@gmail.com on 17 May 2014 at 6:57

GoogleCodeExporter commented 9 years ago
Please see 
http://stackoverflow.com/questions/23708551/struts-2-jquery-plugin-issubscribe-n
ot-working 
and 
http://stackoverflow.com/questions/20441178/struts-2-jquery-subscribe-is-called-
more-than-once

  //instead of $.subscribe('before_grid_load' !!
  $("#gridtable").subscribe('before_grid_load', function(event, data) {
   //do some thing - will be called only once
}

or kill the topic (which I do not prefer it myself)

 $.subscribe('before_grid_load', function(event, data) {
    //do some thing
    $.destroyTopic('before_grid_load');
}

Original comment by afattah...@gmail.com on 17 May 2014 at 3:40