You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Altuğ Bilgin Altıntaş <al...@gmail.com> on 2010/10/22 15:04:44 UTC
sending ajax response part by part
Hi all;
username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
@Override
public void onUpdate(AjaxRequestTarget target) {
String thisUsername = username.getModelObject();
username.add(new SimpleAttributeModifier("class",
"thinking")); // I need repaint
target.addComponent(username); // It doesn't work
// long process
try {
Thread.sleep(2000);
} catch (Exception ex) {
}
// the result so i need to repaint and send to user again
if (ArrayUtils.contains(takenUsernames, thisUsername)) {
username.add(new SimpleAttributeModifier("class",
"approved"));
} else {
username.add(new SimpleAttributeModifier("class",
"denied"));
}
target.addComponent(username);
}
});
How can i send Ajax response part by part; before long process i want to
change css and send it to user, after process i again want to send Ajax
response in one method.
Is it possible ?
Thanks.
Re: sending ajax response part by part
Posted by Altuğ Bilgin Altıntaş <al...@gmail.com>.
By the way this JS file does the same job :
***************************************************
window.onload = initPage;
function initPage() {
/* on start up; assign checkUsername function to onblur */
document.getElementById("username").onblur = checkUsername;
/* disable register button*/
document.getElementById("register").disabled = true;
}
function checkUsername() {
document.getElementById("username").className = "thinking";
request = createRequest();
if (request == null)
alert("Unable to create request");
else {
var theName = document.getElementById("username").value;
var username = escape(theName);
var url = "engine?username=" + username;
request.onreadystatechange = showUsernameStatus;
request.open("GET", url, true);
request.send(null);
}
}
function showUsernameStatus() {
if (request.readyState == 4) {
if (request.status == 200) {
if (request.responseText == "okay") {
document.getElementById("username").className = "approved";
document.getElementById("register").disabled = false;
} else {
document.getElementById("username").className = "denied";
document.getElementById("username").focus();
document.getElementById("username").select();
document.getElementById("register").disabled = true;
}
}
}
}
***************************************************
Altug.
25 Ekim 2010 09:10 tarihinde Altuğ Bilgin Altıntaş <al...@gmail.com> yazdı:
> Thanks Martin;
>
> AjaxTimerBehavior doesn't tackle this issue
>
> I solved the issue by using IAjaxCallDecorator.
>
> Here is the code :
>
> final TextField<String> username = new TextField<String>("username", new
> Model<String>());
> username.setMarkupId("username");
> username.setOutputMarkupId(true);
> form.add(username);
>
>
>
> username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
>
> @Override
> protected IAjaxCallDecorator getAjaxCallDecorator() {
> return new IAjaxCallDecorator() {
> public CharSequence
> decorateOnFailureScript(CharSequence script) {
> return "alert('2');"+script ;
> }
>
> public CharSequence decorateScript(CharSequence script)
> {
> *return
> "document.getElementById(\"username\").className = \"thinking\";"+script ;
> *
> }
>
> public CharSequence
> decorateOnSuccessScript(CharSequence script) {
> return script ;
>
> }
> } ;
> }
>
> @Override
> public void onUpdate(AjaxRequestTarget target) {
> String thisUsername = username.getModelObject();
> username.add(new SimpleAttributeModifier("class",
> "thinking"));
>
> try {
> // long running job - not so long...
> Thread.sleep(2000);
> } catch (Exception ex) {
>
> }
>
> if (ArrayUtils.contains(takenUsernames, thisUsername)) {
> username.add(new SimpleAttributeModifier("class",
> "approved"));
> } else {
> username.add(new SimpleAttributeModifier("class",
> "denied"));
> }
> target.addComponent(username);
> }
> });
>
> Altug.
>
>
> 2010/10/22 Martin Grigorov <mg...@apache.org>
>
> No.
>> The parsing on the client part (wicket-ajax.js) will not start until the
>> whole XML response is delivered.
>>
>> You can use AjaxTimerBehavior to check whether the *slow* calculation is
>> finished and then deliver its response
>>
>> 2010/10/22 Altuğ Bilgin Altıntaş <al...@gmail.com>
>>
>> > Hi all;
>> >
>> > username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
>> > @Override
>> > public void onUpdate(AjaxRequestTarget target) {
>> > String thisUsername = username.getModelObject();
>> > username.add(new SimpleAttributeModifier("class",
>> > "thinking")); // I need repaint
>> > target.addComponent(username); // It doesn't work
>> >
>> > // long process
>> > try {
>> > Thread.sleep(2000);
>> > } catch (Exception ex) {
>> >
>> > }
>> >
>> > // the result so i need to repaint and send to user again
>> > if (ArrayUtils.contains(takenUsernames, thisUsername)) {
>> > username.add(new SimpleAttributeModifier("class",
>> > "approved"));
>> > } else {
>> > username.add(new SimpleAttributeModifier("class",
>> > "denied"));
>> > }
>> > target.addComponent(username);
>> > }
>> > });
>> >
>> > How can i send Ajax response part by part; before long process i want to
>> > change css and send it to user, after process i again want to send Ajax
>> > response in one method.
>> >
>> > Is it possible ?
>> >
>> > Thanks.
>> >
>>
>
>
Re: sending ajax response part by part
Posted by Altuğ Bilgin Altıntaş <al...@gmail.com>.
Thanks Martin;
AjaxTimerBehavior doesn't tackle this issue
I solved the issue by using IAjaxCallDecorator.
Here is the code :
final TextField<String> username = new TextField<String>("username", new
Model<String>());
username.setMarkupId("username");
username.setOutputMarkupId(true);
form.add(username);
username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
@Override
protected IAjaxCallDecorator getAjaxCallDecorator() {
return new IAjaxCallDecorator() {
public CharSequence decorateOnFailureScript(CharSequence
script) {
return "alert('2');"+script ;
}
public CharSequence decorateScript(CharSequence script)
{
*return
"document.getElementById(\"username\").className = \"thinking\";"+script ;*
}
public CharSequence decorateOnSuccessScript(CharSequence
script) {
return script ;
}
} ;
}
@Override
public void onUpdate(AjaxRequestTarget target) {
String thisUsername = username.getModelObject();
username.add(new SimpleAttributeModifier("class",
"thinking"));
try {
// long running job - not so long...
Thread.sleep(2000);
} catch (Exception ex) {
}
if (ArrayUtils.contains(takenUsernames, thisUsername)) {
username.add(new SimpleAttributeModifier("class",
"approved"));
} else {
username.add(new SimpleAttributeModifier("class",
"denied"));
}
target.addComponent(username);
}
});
Altug.
2010/10/22 Martin Grigorov <mg...@apache.org>
> No.
> The parsing on the client part (wicket-ajax.js) will not start until the
> whole XML response is delivered.
>
> You can use AjaxTimerBehavior to check whether the *slow* calculation is
> finished and then deliver its response
>
> 2010/10/22 Altuğ Bilgin Altıntaş <al...@gmail.com>
>
> > Hi all;
> >
> > username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
> > @Override
> > public void onUpdate(AjaxRequestTarget target) {
> > String thisUsername = username.getModelObject();
> > username.add(new SimpleAttributeModifier("class",
> > "thinking")); // I need repaint
> > target.addComponent(username); // It doesn't work
> >
> > // long process
> > try {
> > Thread.sleep(2000);
> > } catch (Exception ex) {
> >
> > }
> >
> > // the result so i need to repaint and send to user again
> > if (ArrayUtils.contains(takenUsernames, thisUsername)) {
> > username.add(new SimpleAttributeModifier("class",
> > "approved"));
> > } else {
> > username.add(new SimpleAttributeModifier("class",
> > "denied"));
> > }
> > target.addComponent(username);
> > }
> > });
> >
> > How can i send Ajax response part by part; before long process i want to
> > change css and send it to user, after process i again want to send Ajax
> > response in one method.
> >
> > Is it possible ?
> >
> > Thanks.
> >
>
Re: sending ajax response part by part
Posted by Martin Grigorov <mg...@apache.org>.
No.
The parsing on the client part (wicket-ajax.js) will not start until the
whole XML response is delivered.
You can use AjaxTimerBehavior to check whether the *slow* calculation is
finished and then deliver its response
2010/10/22 Altuğ Bilgin Altıntaş <al...@gmail.com>
> Hi all;
>
> username.add(new AjaxFormComponentUpdatingBehavior("onblur") {
> @Override
> public void onUpdate(AjaxRequestTarget target) {
> String thisUsername = username.getModelObject();
> username.add(new SimpleAttributeModifier("class",
> "thinking")); // I need repaint
> target.addComponent(username); // It doesn't work
>
> // long process
> try {
> Thread.sleep(2000);
> } catch (Exception ex) {
>
> }
>
> // the result so i need to repaint and send to user again
> if (ArrayUtils.contains(takenUsernames, thisUsername)) {
> username.add(new SimpleAttributeModifier("class",
> "approved"));
> } else {
> username.add(new SimpleAttributeModifier("class",
> "denied"));
> }
> target.addComponent(username);
> }
> });
>
> How can i send Ajax response part by part; before long process i want to
> change css and send it to user, after process i again want to send Ajax
> response in one method.
>
> Is it possible ?
>
> Thanks.
>