hungsama / struts2-jquery

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

Why is targets required for topics to get published? #969

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
Create a form, exactly as described in the documentation for submit, e.g.
<s:form id="formevent" action="echo" theme="simple" cssClass="yform">
        <fieldset>
            <legend>AJAX Form</legend>
                <div class="type-text">
                    <label for="echo">Echo: </label>
                    <s:textfield id="echo" name="echo" value="Hello World!!!"/>
                </div>
                <div class="type-button">
                    <sj:submit  value="AJAX Submit" 
                                        timeout="2500" 
                                        indicator="indicator" 
                                        onBeforeTopics="before" 
                                        onCompleteTopics="complete" 
                                        onErrorTopics="errorState"  
                                        effect="highlight" 
                                        effectOptions="{ color : '#222222' }" 
                                        effectDuration="3000"/>
                </div>
        </fieldset>
    </s:form>

Note the "targets" attribute from sj:submit has been removed.
None of the subscriptions specified in "onBeforeTopics, onCompleteTopics, 
onErrorTopics" will get called.

Which struts2 version?
2.1.8.1

Which struts2-jquery plugin version?
3.5.1

Please provide any additional information below.
In the example too, the targets is specified but there is no element with that 
id.

I have read the documentation and was not able to find the answer, would 
appreciate if someone could point me in the right direction.

Original issue reported on code.google.com by ashishga...@gmail.com on 19 Apr 2013 at 11:32

GoogleCodeExporter commented 9 years ago
The AJAX Request is bound to the target element. Without the Target Element the 
Request is never submitted and the topics are not called.

Original comment by johgep on 25 Apr 2013 at 7:38

GoogleCodeExporter commented 9 years ago
In my case I do not have a target element. I mean the onCompleteTopics takes 
care of showing the effect of the submit action, there is no target element to 
directly affect. Work around I have used is to give it a dummy target element 
that does not exist. In my opinion this could be a use case for a lot of 
people, the target elements requirement to execute the topics should be removed.

Original comment by ashishga...@gmail.com on 28 Apr 2013 at 6:09

GoogleCodeExporter commented 9 years ago
I have the same issue and used exactly a dummy target!
------------------------------------------------
I am going to explain my problem and hope it would be useful for some one :)

Here is my problem:

1- If user submits a form the server may return an error or not.
2- I want to show the error on the same form.

My dummy element was actually a hidden div. 

I developed and interceptor so the server always return the errors in JSON 
format, so if there is an error the result could be parsed with JSON. (This 
logic can be changed, for example you can return a simple fixed text and return 
it to client)

All forms will have a form-error div, which will be used to show form errors.

The interceptor is a try catch which will be as below

        try {
            return invocation.invoke();
        } catch (Throwable ex) {
            response.setCharacterEncoding("UTF-8");
            StringBuilder sb = new StringBuilder();
            sb.append("{\"globalerror\":\" ");
            sb.append(ex.getLocalizedMessage());
            sb.append("\"}");
            response.getWriter().write(sb.toString());
            return Action.NONE;
       }

The jsp:

onSuccessTopics="formServerErrorAnalyze"

The js:

 $.subscribe("formServerErrorAnalyze", function(event, data) {
        var recviedRequest = event.originalEvent.data;
        var errorMessage;

        try {
            //if the form has errors then it will be type of json
            errorMessage = $.parseJSON(recviedRequest);

            $("#form-error").html(errorMessage .globalerror);
        } catch (e) {
            errorMessage="";
        }      
        if (errorMessage===""){
            // There is no error put the content of 'dummy' node to 'content'
            var working = $("#dummy").contents();
            var ref = $("#content").contents();
            $("#dummy").append(ref);
            $("#content").append(working);
        }
        //clean dummy
        $("#dummy").html('');
    });

Original comment by afattah...@gmail.com on 10 May 2014 at 6:12