Hi all! In the hope that I came to the right place in search for answers, I'd like to post what I'm experiencing right now regarding the DOJO toolkit, the "unbeatable JavaScript tools" as they call it. The story is long, so I'll try to stick to the point.
Need to add that I'm using Java/Struts/JSP; in struts-config there are mapped the paths with forms and actions (the action is based on parameter 'dispatch').
What I want to achieve is to download some files. I have a list of owes corresponding to an employee, and each one of this has a corresponding file as proof.
So to each file, there's a button attached in the
JSP code, which loooks like this:
<logic:iterate name="owes" id="element" indexId="index" scope="request">
<tr> <td>
...
<button id="downloadButton${pageScope.element.id}" type="button" dojoType="dijit.form.Button"
onClick='downloadProof("${pageScope.element.id}", arguments[0])'>
<bean:write name="element" property="fileName" />
</button>
So when button is pressed, it goes to the 'downloadProof' method from the Javascript file, and in this case the Dojo toolkit is used.
The initial 'downloadProof' method does this:
function downloadProof(oweId, event)
{
dojo.byId("dispatch").value = "downloadProof";
dojo.byId("oweId").value = oweId;
dojo.io.iframe.send({
form: "ldfApprovalForm",
handleAs: "html",
load: function(response, args)
{
return response;
},
error: function(response, args)
{
dojo.byId("errorAjaxMessage").innerHTML = dojo.toJson(response);
dijit.byId("errorAjaxDialog").show();
return response;
}
});
}
Aparently all code's allright, the JSP, the JS, the action in the
java class, connection to server & file download when you first try it.
this JS method should call the 'downloadProof' method from the action associated to the form.
Problem appears when you try to
open/download more than one file. I mean, I have the list of buttons, the first one that I press runs perfectly, and after this pressing it has absolutelly no effect.
From what I read, the problem seems to be
dojo.io.iframe.send, because, as some say, loads only once per page load OR it simply doesn't know when it has finished doing it's job, so the rest of calls will be stored in a queue - couldn't figure it out what makes the next in queue to trigger (
http://api.dojotoolkit.org/jsdoc/1.3.2/dojo.io.iframe.send). And it makes sense, the behaviour I get conforms to all these.
In search for solutions now. Found some suggestions here (
http://o.dojotoolkit.org/forum/dojo-core-dojo-0-9/dojo-core-support/dojo-io-iframe-send-problem), nothing works, and the timeout with refresh doesn't apply to my page, as I have disabled saving the content in cache, and because on load many things happen in the background of my page, which makes refresh very unconvenient.
Then, because of this
http://www.mikejuniper.com/2009/03/fun-with-dojoioiframesend/comment-page-1/#comment-1247, I've started to implement a DEFERRED object, and cancel it before making another iframe.send call. So now my method turned into this:
function downloadProof(oweId, event)
{
if (this.deferred)
{
this.deferred.cancel();
this.deferred = new dojo.Deferred();
}
dojo.byId("dispatch").value = "downloadProof";
dojo.byId("oweId").value = oweId;
this.deferred = sendF();
}
function sendF(callback)
{
var def = new dojo.Deferred();
dojo.io.iframe.send({
form: "ldfApprovalForm",
handleAs: "html",
method: "post",
load: function(response, args)
{
def.callback(true);
return response;
},
error: function(response, args)
{
dojo.byId("errorAjaxMessage").innerHTML = dojo.toJson(response);
dijit.byId("errorAjaxDialog").show();
return response;
}
});
def.callback(true);
return def;
}
function downloadPFError()
{
var def = new dojo.Deferred();
var s = "In ERR handling; response.message: " ;
alert(s + "deferred's value:"+deferred);
def.callback(true);
return def;
}
I wasn't able to understand the documentation presented, and I couldn't find a proper example of CANCELLING the DEFERRED object. I don't understand what is suppose to happen to a canceled deferred, as this is pretty vague to me:
http://api.dojotoolkit.org/jsdoc/1.3.2/dojo.Deferred.
I don't know if I have implemented well the errback on cancel. From what I experience, my code goes into downloadPFError (the addErrback of the deferred) only ONCE. And again, it enters IFRAME.SEND only once, the first trial, and I cannot get more than one file.
The official Dojo forum is closed, not to mention that the API and resources found on Internet are extremelly poor.
I've even started to read the API from TWISTED, as I understand that the Deferred method has been inspired by this and honestly, I understood more from here than from the Dojo API. If I see another example of deferred that only involves setTimeout I'll go coocoo.
All apologies if this is not the right place for this, and please you all don't take all this the wrong way, as you can tell I'm getting a little impacient.
So everybody reading this, have you experienced anything similar?
If you have more experience in this, can you please tell me what's wrong with my code?
Where can I find a complete example of a Deferred object, that is cancelled? (besides looking into the source code of pages on internet)
And what can I do or use to be able to download more than one file?
Thanks to all in advance, I appreciate it.
Laura