David Morton wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I''ve been working on several ajax problems, and need some help from some > others who have been working on similar items. In addition, I have some > ideas that we might want to develop upon. This is a long post, so feel > free to chop it up and start a new thread if needed. I hope it makes > sense; this is a somewhat complicated subject. :) > > I''m not very fluent in javascript, so having helper methods to write it > for me is very cool.To a point. It helps to actually understand what generated code is doing, and how to change it to suit your needs. If you can hack Ruby, then JavaScript should be easy enough for you to pick up.> > The first problem I ran into was when I needed to update multiple > locations within a page. The current link_to_remote allows you to > specify one target. This specification exists in the page view, on the > client. What happens if you wnat to update multiple locations? Or > update one location for one set of data, but update another location for > another set of data?Have you considered hitting a single end-point and pulling back the complete XML you need for your page? You can cache it in a javascript variable, then let the client extract the appropriate parts for the various page sections. Eliminates multiple client/server calls. James
David Morton wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I''ve been working on several ajax problems, and need some help from some > others who have been working on similar items. In addition, I have some > ideas that we might want to develop upon. This is a long post, so feel > free to chop it up and start a new thread if needed. I hope it makes > sense; this is a somewhat complicated subject. :) > > I''m not very fluent in javascript, so having helper methods to write it > for me is very cool.To a point. It helps to actually understand what generated code is doing, and how to change it to suit your needs. If you can hack Ruby, then JavaScript should be easy enough for you to pick up.> > The first problem I ran into was when I needed to update multiple > locations within a page. The current link_to_remote allows you to > specify one target. This specification exists in the page view, on the > client. What happens if you wnat to update multiple locations? Or > update one location for one set of data, but update another location for > another set of data?Have you considered hitting a single end-point and pulling back the complete XML you need for your page? You can cache it in a javascript variable, then let the client extract the appropriate parts for the various page sections. Eliminates multiple client/server calls. James
David Morton wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > I''ve been working on several ajax problems, and need some help from some > others who have been working on similar items. In addition, I have some > ideas that we might want to develop upon. This is a long post, so feel > free to chop it up and start a new thread if needed. I hope it makes > sense; this is a somewhat complicated subject. :) > > I''m not very fluent in javascript, so having helper methods to write it > for me is very cool.To a point. It helps to actually understand what generated code is doing, and how to change it to suit your needs. If you can hack Ruby, then JavaScript should be easy enough for you to pick up.> > The first problem I ran into was when I needed to update multiple > locations within a page. The current link_to_remote allows you to > specify one target. This specification exists in the page view, on the > client. What happens if you wnat to update multiple locations? Or > update one location for one set of data, but update another location for > another set of data?Have you considered hitting a single end-point and pulling back the complete XML you need for your page? You can cache it in a javascript variable, then let the client extract the appropriate parts for the various page sections. Eliminates multiple client/server calls. James
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 James Britt wrote: | Have you considered hitting a single end-point and pulling back the | complete XML you need for your page? You can cache it in a javascript | variable, then let the client extract the appropriate parts for the | various page sections. Eliminates multiple client/server calls. This method only makes one call. The response can have multiple commands and thus take multiple actions, but it is only one request/response. The problem is being able to specify multiple targets within a form. To do that, I need to change the target of the form, which means I need to find the DOM element of the form. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCUgxESIxC85HZHLMRArA/AJ91TmffSV2ARFQpkM/i/BUc3jK1egCfSFSb dSDy1MTU1RS5GNvMiTs0eks=gHVg -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 David Morton wrote: | The problem is being able to specify multiple targets within a form. To | do that, I need to change the target of the form, which means I need to | find the DOM element of the form. I figured out one way. in the from_remote tag function, I added: :html => {:id => "txn_bank"} which sets an id tag for the form. Then in my link_to, I can reference it to serialize the form: <%= link_to_remote "Save for autopayment", ~ {:method => "''post''", ~ :with => "Form.serialize($(''txn_bank''))", ~ :complete => ''eval(request.responseText);'', ~ :url => {:action => "save_auto_payment_ajax", ~ :id => @customer.id}, ~ :confirm => "Add/Update this payment to autopayments?"} %> Note the presence of the :method and :with parameters. Now the form data is serialized and posted back to the remote function, the output of which is eval''d. Life is good. Well, sorta. I''d like to see that call made a bit more simple. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCUiA/SIxC85HZHLMRAkx2AJ4mBaBpzcAAnNJHfSrevOznAH87XACgpUS5 Y2+pCyz1EJ+srdgsFJj9+R0=VluL -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I''ve been working on several ajax problems, and need some help from some others who have been working on similar items. In addition, I have some ideas that we might want to develop upon. This is a long post, so feel free to chop it up and start a new thread if needed. I hope it makes sense; this is a somewhat complicated subject. :) I''m not very fluent in javascript, so having helper methods to write it for me is very cool. The first problem I ran into was when I needed to update multiple locations within a page. The current link_to_remote allows you to specify one target. This specification exists in the page view, on the client. What happens if you wnat to update multiple locations? Or update one location for one set of data, but update another location for another set of data? To work around this, I implemented a helper called (unimaginatively) send_ajax. It is available as a patch at http://dev.rubyonrails.com/ticket/933 To use this function, declare your link_to_remote with no :update target, but rather use the complete action: :complete => ''eval(request.responseText);'' By evaluating the response, you can delegate the view update instructions to the server. The server needs only to send back a set of ~ javascript instructions on what components to update within the view. This is where send_ajax comes in... From a template on the server, it escapes a block of html text into a javascript call to replace or insert into the given target. A series of these statements and/or straight javascript can then be returned to the client for execution. Thus, for example, when I submit a payment from one form on the page, I am able to update a transaction list in one corner of the page, an account balance in the title of another section, and a text field of memos in another. OTOH, if there is an error with the submission, I can place the error message in a small div located right above the form I just submitted. To achieve this, I render the following template with no layout: <% @foo = send_ajax :action => :replace, :target => "edit_transactions" ~ do %> <%= render_partial ''edit_transactions'' %> <% end %> <%= @foo %> $(''edit_transaction_<%= @txn.id %>'').scrollIntoView(false); $(''edit_account_balance'').innerHTML = ''<%number_to_currency(@customer.account_balance) %>'' $(''customer_memo'').value = ''<%= @customer.memo %>'' <% @foo = send_ajax :action => :replace, :target => "txn_flash" do %> <font color="red"> updated </font><% end %> <%= @foo %> The only thing I don''t like with this helper is the fact that I can''t do a <%= send_ajax... The ''='' causes a syntax error. Instead, I have to assign send_ajax to a variable, and then do a <%= @foo %> I''d appreciate it if someone who uderstands erb stuff would look at it and tell me what I''m missing. Another situation I''m facing involves how to submit form data back to a remote method without having to use the submit button. Or.. to submit to a different method. Perhaps one way would be to implement a submit button that rewrites the target of the form submission when clicked. But if one tries to use link_to_remote within a form, it doesn''t send the contents of the form. It appears this has to do with the From.serialize method, which needs to operate on a form object. Is there an easy way to get that object? currently, it appears there''s no way to pass an :id to form_remote_tag, nor does it generate an id tag. Otherwise it might be possible to have a link_to tag include the contents of the form with :with => "Form.serialize($(''form_id''))" Does that look right to any javascript gurus out there? Any other ideas on how to solve this? Next, I did work around this in one other problem... I have a select box that when something is selected, I want to pull data out of the database and place it in a form. Here''s what I ended up with: <%= collection_select(''plan'', ''name'', @plans, ''id'', ''name'', {}, ~ {:onchange => remote_function( ~ :url => { :action => "get_plan_with_ajax", :id => @customer.id}, ~ :with => "Form.Element.serialize(this)", ~ :method => "''post''", ~ :loading => ''$(\''mem_ajax_status\'').innerHTML = \''loading ...\'''', ~ :complete => ''eval(request.responseText);$(\''mem_ajax_status\'').innerHTML = \'' \'''' ~ )}) %> As you can see that''s still kinda ugly, and there should be a better way to say all that. The key points that I had difficulty with are the :method and :with parameters. I don''t know why the :method call required a double quote, but it did. The :with parameter fooled me for a while, until I realized I just needed the Element that this call is attached to, rather than the full form. So what I''m looking for is some ideas on how to make some of these operations easier. I highly recommend using eval(request.responseText) to act upon the server response, which gives the programmer much more control over the view, allowing for multiple, conditional updates. I''m looking for some ideas on how to make the syntax easier, and how to access the form data within a link_to or button tag. I''m guessing this involves some work to make form tags have an id tag somehow, and then improving :with to look for the containing form DOM element. Oh, and one other suggestion. If we had some sort of gzip support built into Prototype.js, we could gzip the response from the server and unzip it in the client before eval''ing it, which would conserve even more bandwidth. - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCUk1ySIxC85HZHLMRAmu4AJ9qGXeJMqLpF5uPwC/Kq36VTZ7nTwCfXDQc GS6ppJOvqLdh1gCMmUivbqc=ZUq5 -----END PGP SIGNATURE-----