On 7/31/05, Bob Ippolito <
b...@redivi.com> wrote:
> I hope you don't mind me copying this to the list now that I have one
> setup...
Not at all. I had actually wondered about a mailing list when I was
sending this to you.
> On Jul 30, 2005, at 8:37 AM, Kevin Dangoor wrote:
> > MochiKit.Async.doXMLHttpRequest = function(url, optionsInput) {
> > options = {
> > method : "GET",
> > async : true,
> > decode : null,
> > parameters : null
> > };
> > MochiKit.Base.update(options, optionsInput);
> >
> > }
> I don't really see the benefit of a decode function (much less a
> registry of string->function), that's really what addCallback is
> for. The function suitable for JSON callbacks is simply
> MochiKit.Async.evalJSONRequest. The reason things are what they are
> is because the code for MochiBot deals only in JSON documents via GET
> urls with nothing fancy required along the way. However, the
> JavaScript code is simply asking for information, it never causes any
> state change on the server. All of our state changing operations are
> still traditional HTML form POST operations.
You're right, addCallback should be just fine. Let me make sure I'm
clear on usage:
d = doXMLHttpRequest(options...);
d.addCallback(evalJSONRequest)
d.addCallback(doMyFancyApplicationThing)
d.addErrback(somethingBadHappened)
doMyFancyApplicationThing will get called with the object that
resulted from the JSON eval.
> The rest of the idea I like though. Options objects are great if you
> have more than two or three things you can specify and there is no
> obvious order to default them in... MochiKit just doesn't currently
> have much code in there yet where that makes sense (but this is
> certainly a case where it does).
Yep. I gathered what your usage scenarios were like based on what
MochiKit had. I've already had to deal a little bit with the pain of
URL building in JavaScript, and I'd really like to see that logic
incorporated into a nice handy function that basically amounts to "get
my data from the server".
> > The trick with this is the return value: synchronous requests really
> > only need the XMLHttpRequest object back, whereas async needs a
> > Deferred. My inclination is for this function to always return a
> > Deferred. If need be, there can be a single wrapper function for
> > synchronous requests that very clearly returns the req object.
> >
> > I wanted to get your opinion on this before I actually write
> > something.
>
> Does a synchronous XMLHttpRequest end up calling onreadystatechange?
> If so, I guess async/sync can be the same function and both can
> return a Deferred.
>
> If not, then they should probably be separate functions (though they
> will certainly share a common code path).
Synchronous XMLHttpRequest does not call onreadystatechange. It just
returns once the request is done. I'm not a fan of two distinctly
different possible return values for a function, so I agree that they
should be separate.
> One thing I would love to see synchronous requests for is a
> MochiKit.Logging observer to more or less replace alert() (i.e. works
> even if the JavaScript code totally gets hosed or the browser crashes).
That could be cool, I agree. Luckily for me, I haven't crashed the
browser with my JavaScript yet :)
Kevin