Struts 2 jQuery Subscribe is called more than once

我与影子孤独终老i 提交于 2019-12-13 00:26:08

问题


In the struts 2 jQuery plugin there is a publish/subscribe framework which can be used for event invocation.

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!

Also I did not find any way to call and use this method.


回答1:


every time I reload the page the subscribe is called and the code in the subscribe runs again.

Of course, that's the standard behavior. If you need something to be ran only once, in the entire life-cycle, you can use session/cookies.

onGridCompleteTopics - This means everytime the grid loads the topic is published. And everytime the page is reloaded/refreshed the grid is loaded, publishing the event.

Hope so it clears your doubt.




回答2:


A solution was found at Struts 2 jQuery plugin isSubscribe not working

One can use

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

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

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

The isSubscibe is still there and it is attached to document, please see Struts 2 jQuery plugin isSubscribe not working for complete code by @Roman C




回答3:


I had the same issue for onEditInlineSuccessTopics and replacing

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

with

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

was the best solution that worked for me.

The other version related to destroyTopic, made the topic work only the first time I edited a row and if I wanted to edit another row, the topic was not called at all.




回答4:


Just use return false; at the end of function.

$.subscribe('grid_compelete', function(event, data) {
    return false;
});


来源:https://stackoverflow.com/questions/20441178/struts-2-jquery-subscribe-is-called-more-than-once

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