Hi -- I''m working on something which has a form field to observe, and corresponding stuff happening in a Flash widget which it also needs to stay up to date on. What I''ve been doing is using Form.Element.Observer to track the form field, and attempting to use JS variables to stay up 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 tried 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. The new approach I''m about to set up is a form with hidden tags that get manually reset by custom JS and a Form.Observer object. Then, every time the other data outside the form gets changed, I change the hidden tags, the Form.Observer notices the change, and blam, it updates as it''s supposed to, and everything works. I just want to run this approach by the people on this list, and see if it sounds sensible to you. Form.Element.Observer is obviously built only to observe one field at a time, and Form.Observer was obviously built to track all the fields in a form -- not built to track one form field and some data concerning a Flash widget. However, it seems like the best way to coordinate these things, unless some method of creating an Ajax.Request with dynamic parameters exists. Thanks, Giles http://www.gilesgoatboy.org http://gilesbowkett.blogspot.com http://gilesgoatboy.blogspot.com --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hello there, giles goat 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: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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 <t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org> 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: t...-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
> 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.Wow. I''m sorry, I didn''t even see how good this was. This would have been a much nicer solution. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---