Yes, you'll have to stop the event anyway.
Then, from your onFailure callback do something like:
onFailure: function() {
$('myform').submit();
}
You'll still be missing cases where the request timed out or when the
server was down.
Best,
Tobie
On Dec 29, 7:58 pm, Turner <[EMAIL PROTECTED]> wrote:
> I think I may have misunderstood your previous post. The way I read it
> was the following:
>
> My submit function was firing off an AJAX request, but the AJAX
> request doesn't block so once it fired the request, it exited my
> function. The AJAX request not being complete, the event hadn't been
> stopped, so the browser goes merrily on its way submitting the form,
> and my onSuccess function stops the event afterwards, only it's too
> late as the form has been submitted, so stopping the event is useless.
> I assumed that Form#request() did some sort of blocking magic under
> the hood--i.e., it would not allow the default action until the
> request had been fully processed-- although I don't know why I did,
> looking back.
>
> What you just wrote is basically what I have, only I have the event
> stopped inside the onSuccess function (I guess that makes a huge
> difference, though, huh?). The problem is, I only want to stop the
> event if the AJAX request was successfully sent and processed;
> submitting the form should be a fallback in case AJAX is not supported
> or something went wrong in the request. So I don't want to
> unconditionally stop the event; that's why I placed it in my onSuccess
> function. Any way to do that that you know of?
>
> Thanks,
> Turner
>
> On Dec 29, 1:27 pm, Tobie Langel <[EMAIL PROTECTED]> wrote:> Hi again,
>
> > If you need a different url, why don't you just use the Ajax.Request
> > object like so:
>
> > $('my_form').observe('submit', function(event) {
> > var valid = validate();
> > if (!valid) return;
> > new Ajax.Request('myurl', {
> > onSuccess: someFunc,
> > parameters: this.serialize(true)
> > });
> > event.stop();
>
> > });
>
> > I suggest you have a look at our
> > tutorial:http://prototypejs.org/learn/introduction-to-ajax
>
> > Best,
>
> > Tobie
>
> > On Dec 29, 5:56 pm, Turner <[EMAIL PROTECTED]> wrote:
>
> > > Hi Tobie,
>
> > > Ah, thank you--that looks very much like what I want (I didn't know
> > > that method was there!). I just have one problem: I want the AJAX to
> > > post to a different script than the normal form would go to. Since
> > > Form#request() takes its URL from the form's action, which is not the
> > > script the AJAX should go to, I would need to change that somehow. I
> > > suppose I could hack it and change the action attribute with
> > > writeAttribute() before AJAX, then change it back afterwards and hope
> > > that Form#request() gets the action at the right time (or set a
> > > timeout), but I would hope there's a better way to override the URL it
> > > requests. Do you know of such a way? I don't see it in the list of
> > > standard Ajax#request() options.
>
> > > Thank you very much,
>
> > > Turner
>
> > > On Dec 29, 5:08 am, Tobie Langel <[EMAIL PROTECTED]> wrote:
>
> > > > I've re-read your initial post. It appears that you are stopping the
> > > > event from your onSuccess callback, that's abviously way too late
> > > > (remember, the "a" in ajax stands for asynchronous).
>
> > > > In pseudo code, your observer should look something like this:
>
> > > > $('my_form').observe('submit', function(event) {
> > > > var valid = validate();
> > > > if (!valid) return;
> > > > this.request({onSuccess: someFunc}) // this stands for your form
> > > > element here. seehttp://prototypejs.org/api/form/request
> > > > event.stop();
>
> > > > });
>
> > > > Best,
>
> > > > Tobie
>
> > > > On Dec 29, 10:27 am, Turner <[EMAIL PROTECTED]> wrote:
>
> > > > > Hi Tobie,
>
> > > > > I feel appropriately foolish for that. I <i>know</i> about onsubmit--
> > > > > why did I choose onclick? Bah. Well, I'm sad to report that, after
> > > > > changing all my relevant observations to 'submit', I am still getting
> > > > > the same results. It seems that should have done the trick, but sadly,
> > > > > not. Thank you for your tip, though.
>
> > > > > Turner
>
> > > > > On Dec 29, 3:51 am, Tobie Langel <[EMAIL PROTECTED]> wrote:
>
> > > > > > Hi,
>
> > > > > > You should be observing the form's submit event not the submit
> > > > > > button's click event.
>
> > > > > > Best,
>
> > > > > > Tobie
>
> > > > > > On Dec 29, 8:19 am, Turner <[EMAIL PROTECTED]> wrote:
>
> > > > > > > I've just started using Prototype, and it's mostly been pretty
> > > > > > > good to
> > > > > > > me. I just have one strange problem. I'm observing a click event
> > > > > > > on a
> > > > > > > comment submit button in order to run a function before it
> > > > > > > submits a
> > > > > > > form. That function, among other things like validation, submits
> > > > > > > an
> > > > > > > AJAX request to add to the database. The idea is that it should
> > > > > > > do the
> > > > > > > AJAX request if the browser supports it and then stop the event
> > > > > > > from
> > > > > > > submitting the form (if there is no AJAX support, it should go
> > > > > > > ahead
> > > > > > > and submit it).
>
> > > > > > > So I'm using Prototype's Ajax functions, and I have an onSuccess
> > > > > > > function that does its stuff with the server response and then
> > > > > > > stops
> > > > > > > the event using event.stop(). Well, I'm having mixed results--the
> > > > > > > AJAX
> > > > > > > goes through fine, but the form is still being submitted, in
> > > > > > > addition
> > > > > > > to the AJAX request. The event claims to be stopped (that is,
> > > > > > > event.stopped is true after I call event.stop(), as it should
> > > > > > > be), so
> > > > > > > it doesn't seem like a bug there (besides, I tried calling
> > > > > > > event.preventDefault() just in case, and got the same results).
> > > > > > > Not
> > > > > > > that I thought it would be, but you know, try everything, right?
>
> > > > > > > Anyway, this is pissing me off and after hours of debugging I have
> > > > > > > gotten no results. Of course, this probably means that it's some
> > > > > > > ridiculously trivial fencepost error or something of that nature,
> > > > > > > but
> > > > > > > I thought maybe someone here would have some idea of what could be
> > > > > > > going wrong.
>
> > > > > > > Oh, and I get this behavior on IE7 (Windows Vista) and Firefox
> > > > > > > 2.0.0.11 (Vista and Ubuntu). Haven't tested other browsers.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---