OK, been a while, but I appreciate the help. Just for posterity, here's
what I did.
I needed state changes in the Flash app to be reflected immediately or
near-immediately with corresponding state changes on the JavaScript
side and on the Rails side (i.e. the server) as well.
I started with a form field observer, which automatically creates an
Ajax.Request, and then hard-coded the Ajax.Request instead, but it
didn't work either time. The problem was that I wanted the Flash widget
to be able to modify certain variables in real-time via the
Flash-JavaScript bridge, and have those vars submitted in the Request,
but giving Ajax.Request variables to use in this way was totally
impossible, because the Request was constructed with those variables
interpolated at construction time, not with the understanding that they
should be evaluated when the Request was actually made.
I looked around in the Prototype source but we were under the gun and
there's some pretty deep code in there, I didn't feel confident
modifying it.
I tried a couple different approaches, and I ended up representing the
variables as hidden tags in the same form as the original field my form
field observer had been attached to. The Flash side triggered a small
JS function which just modified those hidden fields, instead of setting
JS variables, and the form field observer, I replaced that with a form
observer.
So, boom, any change to the hidden tag, the entire interface, both
Flash and Ajax, can be updated immediately, and of course Rails on the
server is updated as well.
Managing state in a Rails app with Ajax and Flash is kind of inherently
weird, but it's definitely possible, and can be quite cool when it
works. I've run into similar problems during the past, although I
definitely think these are still kind of edge case scenarios, but
perhaps edge cases on the edge of becoming more common. So I think I'm
going to blog this just in case. Anyway, for the record, that's the
solution I came up with. It's a hack, but a relatively streamlined one.
On Dec 13, 1:10 am, Christophe Porteneuve <[EMAIL PROTECTED]> wrote:
Hello there,
gilesgoat boy a écrit :
> to date on the Flash widget stuff. First I tried to put the JS vars in
> the "parameters" var of the new Ajax.Request in the
> Form.Element.Observer, but it appears to be evaluating those vars
> immediately, rather than passing them to Ajax.Request with the idea
> "these are vars to evaluate every time you make a request". I triedWhich is
the nominal behavior of JS: if you use variables at some point,
they get evaluated immediately.
> storing the field observer in a variable and creating a new one each
> time, which goes into that variable, on the hopes that would cause
> garbage collection to destroy the old field observers each time the var
> was reallocated, or whatever, but that didn't work either.
> In fact, what happened there, I ended up with a new observer with the
> correct Ajax.Request parameters each time -- but the old ones weren't
> destroyed, so I had a huge problem on my hands.I'm not too sure how you go
about this. No observer class wires
automatically to an Ajax call: it just invokes its callback method, as
passed at construction time (third argument for time-based observers,
second one for event-based observers).
So this method you're passing could very well build the new Ajax.Request
object and pass it your JS variables among the parameters, which would
get evaluated dynamically at the moment the line is run.
Ajax.Request objects are not designed to be reused, so creating a new
one in the callback function every time it's needed is no problem. As a
local variable (or not even a variable!), it will get garbage-collected
properly.
Something like:
var flashState1 = 'blah';
var flashState2 = 42;
new Form.Element.Observer('myFormElementID', 500, function(elt, value) {
new Ajax.Request('/your/server/script', {
// whatever other options you use, e.g. method, onSuccess...
parameters: { value: value, fs1: flashState1, fs2: flashState2 }
});
});And there you go! Since the callback's body is evaluated every time the
observer detects a change, it will take the latest values for your
global variables.
BTW, this is based on 1.5 RC2.
--
Christophe Porteneuve a.k.a. TDD
"[They] did not know it was impossible, so they did it." --Mark Twain
Email: [EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---