This is where we talk about features and changes to the Ajax implementation in Rails.
Note: The Prototype site has information on how to access the Prototype darcs repository.
There’s talk on the internets (well.. the rails-list anyway) about keeping things out of the ‘core’ and into things like generators or even extra packs.. should all the ajax code go out and be called with something like script/generate ajax?
madrobby: There should be a dedicated add-in mechanism for Rails, not just the lib and components dir. Maybe this would be nice for other things, too.
madrobby: I’m currently working on autocompleting Textfields and some other spezialized ajax helpers, and integration of it into the other helpers (like formhelper)._
Update: Here is the patch for autocompleting textfields: http://dev.rubyonrails.com/ticket/960 .
Have a look at what it can do at at my blog
madrobby: wouldnt it be better if respondto\ReadyState would call the functions with “this” instead of “this.transport”, so you could query the Ajax.Request object?
there could be an alias function for responseText…
I’d like to see multiple responses. For example, let’s say we have an application with a summary list at the top, and a detail view at the bottom. We use ajax to let people edit in the detail view. But if we want the summary view to update as well, then we’re forced to update the collection of partials, and then the user has to click the relevant item in the summary to get the detail view back again (or we go to some trickery to hold that state somewhere).
If instead update took a hash of id’s and in my views I could render several fragments wrapped via some sort of xml envelope, it’d be much more verstile.
mojombo: I’ve written an application helper method and javascript function that allows an action to render multiple partials (or anything) to xml that is then parsed automatically on the Ajax response to update multiple HTML elements. This works great for handling form validation failure. See Rails Ajax Form Error Handling
ebb: see also rjs http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates
dgm: Instead of specifying the target of the update in the link_to_remote helper, use the :complete target to eval the response from the server, which is a javascript fragment. Thus, the server can specify to update as many segments as you want. A helper could be made to assist in this. For example, imagine a partial that consists of:
<% ajax_update "messages" { %>
html that goes in div id "messages"
<% } %>
The link_to_remote could provide hints in the url for the server to use, but the server could decide to do it’s own thing, for example, issue a flash message due to an error.
dgm: I’ve submitted some code fragment that implement this behaviour in http://dev.rubyonrails.com/ticket/933 and http://dev.rubyonrails.com/ticket/940
There could be variants on the :complete parameter, like:
:complete_successful => ''
:complete_failed => ''
For this to work, there must be some standard value returned if the operation succeeds, like ‘OK’ or something, an generated javascript that goes like this:
if(request.responseText=='OK')
{ /* complete_successful */ } else
{ /* complete_failed */ }
Maybe it could check on the “201 CREATED” HTTP header.
LeeO: responseText is not accessible from all browsers, it’s better to use .status to get the status code (just the number). Still, I’m not sold on hooking JS to HTTP status codes, it seems somehow wrong. Of course, having the Ajax object 404 and 500 aware isn’t a bad thing!
As the intention behind “XmlHttpRequest” was using XML to encode the data responded, it may be a good idea to use an already established method for the XML encoding – like XML-RPC.
I’ve put together a small how-to on this. It shows how to update a select list with the id and name of a newly created ActiveRecord object:
Ajax responses with XML-RPC
It also shows how XML-RPC faults can be used for error handling in the view.
Where an Ajax request is simply passed a URL, it should degrade and use
<a href='url' onclick='return function();'> </pre>rather than not work at all. .. and form submits over ajax should submit a POST request if not available. note: this didn’t happen in .10, may be fixed in .11
dgm: Yes, indeed. In fact, I propose that the calling convention should be to add a parameter to all ajax calls: ajax=true. That way, the action can decide which view to render. If the parameter ajax=true is missing, then the controller knows that the client cannot handle ajax, and do the right thing.
Saving the back button?
In defence of the back button explains a technique for maintaining proper back-button behaviour.
Making the Back Button Dance might also inform a potential solution for creating a back-button friendly ajax setup.
Browser compatibility
Safari UTF-8 support
Safari doesn’t correctly interpret the charset header of returned data in responseText. (in fact, it’s hardcoded to ‘ISO-8859-1’). A workaround is using an after_filter, which is described in more detail on HowToUseUnicodeStrings.
Safari & Apache
As of 0.11.1, Safari and Apache play nice again. No need to keep using WEBrick or Firefox (although it helps to still use Firefox for the javascript debugger)
Support of onreadystatechange Events
Events specified by :loading are never called by Safari 1.2.4 (another Bug), so it would have to be emulated by Ajax.Request.
readyState 1 is already emulated by Ajax.Request and works in Safari. — SamStephenson
Actually, the events are not widely supported across all browsers.
in IE you tend not to get anything between 1 and 4; it may be wise to only rely on 1 and 4 (initialized and complete).
For a complete listing of the bugs, see
http://jpspan.sourceforge.net/wiki/doku.php?id=javascript:xmlhttprequest:behaviourAnd readystate 3, from the same page:
Mozilla re-invokes readyState 3 for every 4096 bytes of data received (or end of file – whichever comes first). Meanwhile IE invokes readyState 3 once then waits until the full response has been received before switching to readyState 4
Special Effects
Please see VisualEffects.
Drag and drop
Could something like this be added?
dgm: Along with the edit in place on that page too?
I think the resortable, editable lists would be a big win. There are lots of times when that kind of sortability and in-place editability could be used.bram: I think the edittable slideshow (on the editinplace page) is also very usable…
This is where we talk about features and changes to the Ajax implementation in Rails.
Note: The Prototype site has information on how to access the Prototype darcs repository.
There’s talk on the internets (well.. the rails-list anyway) about keeping things out of the ‘core’ and into things like generators or even extra packs.. should all the ajax code go out and be called with something like script/generate ajax?
madrobby: There should be a dedicated add-in mechanism for Rails, not just the lib and components dir. Maybe this would be nice for other things, too.
madrobby: I’m currently working on autocompleting Textfields and some other spezialized ajax helpers, and integration of it into the other helpers (like formhelper)._
Update: Here is the patch for autocompleting textfields: http://dev.rubyonrails.com/ticket/960 .
Have a look at what it can do at at my blog
madrobby: wouldnt it be better if respondto\ReadyState would call the functions with “this” instead of “this.transport”, so you could query the Ajax.Request object?
there could be an alias function for responseText…
I’d like to see multiple responses. For example, let’s say we have an application with a summary list at the top, and a detail view at the bottom. We use ajax to let people edit in the detail view. But if we want the summary view to update as well, then we’re forced to update the collection of partials, and then the user has to click the relevant item in the summary to get the detail view back again (or we go to some trickery to hold that state somewhere).
If instead update took a hash of id’s and in my views I could render several fragments wrapped via some sort of xml envelope, it’d be much more verstile.
mojombo: I’ve written an application helper method and javascript function that allows an action to render multiple partials (or anything) to xml that is then parsed automatically on the Ajax response to update multiple HTML elements. This works great for handling form validation failure. See Rails Ajax Form Error Handling
ebb: see also rjs http://www.codyfauser.com/articles/2005/11/20/rails-rjs-templates
dgm: Instead of specifying the target of the update in the link_to_remote helper, use the :complete target to eval the response from the server, which is a javascript fragment. Thus, the server can specify to update as many segments as you want. A helper could be made to assist in this. For example, imagine a partial that consists of:
<% ajax_update "messages" { %>
html that goes in div id "messages"
<% } %>
The link_to_remote could provide hints in the url for the server to use, but the server could decide to do it’s own thing, for example, issue a flash message due to an error.
dgm: I’ve submitted some code fragment that implement this behaviour in http://dev.rubyonrails.com/ticket/933 and http://dev.rubyonrails.com/ticket/940
There could be variants on the :complete parameter, like:
:complete_successful => ''
:complete_failed => ''
For this to work, there must be some standard value returned if the operation succeeds, like ‘OK’ or something, an generated javascript that goes like this:
if(request.responseText=='OK')
{ /* complete_successful */ } else
{ /* complete_failed */ }
Maybe it could check on the “201 CREATED” HTTP header.
LeeO: responseText is not accessible from all browsers, it’s better to use .status to get the status code (just the number). Still, I’m not sold on hooking JS to HTTP status codes, it seems somehow wrong. Of course, having the Ajax object 404 and 500 aware isn’t a bad thing!
As the intention behind “XmlHttpRequest” was using XML to encode the data responded, it may be a good idea to use an already established method for the XML encoding – like XML-RPC.
I’ve put together a small how-to on this. It shows how to update a select list with the id and name of a newly created ActiveRecord object:
Ajax responses with XML-RPC
It also shows how XML-RPC faults can be used for error handling in the view.
Where an Ajax request is simply passed a URL, it should degrade and use
<a href='url' onclick='return function();'> </pre>rather than not work at all. .. and form submits over ajax should submit a POST request if not available. note: this didn’t happen in .10, may be fixed in .11
dgm: Yes, indeed. In fact, I propose that the calling convention should be to add a parameter to all ajax calls: ajax=true. That way, the action can decide which view to render. If the parameter ajax=true is missing, then the controller knows that the client cannot handle ajax, and do the right thing.
Saving the back button?
In defence of the back button explains a technique for maintaining proper back-button behaviour.
Making the Back Button Dance might also inform a potential solution for creating a back-button friendly ajax setup.
Browser compatibility
Safari UTF-8 support
Safari doesn’t correctly interpret the charset header of returned data in responseText. (in fact, it’s hardcoded to ‘ISO-8859-1’). A workaround is using an after_filter, which is described in more detail on HowToUseUnicodeStrings.
Safari & Apache
As of 0.11.1, Safari and Apache play nice again. No need to keep using WEBrick or Firefox (although it helps to still use Firefox for the javascript debugger)
Support of onreadystatechange Events
Events specified by :loading are never called by Safari 1.2.4 (another Bug), so it would have to be emulated by Ajax.Request.
readyState 1 is already emulated by Ajax.Request and works in Safari. — SamStephenson
Actually, the events are not widely supported across all browsers.
in IE you tend not to get anything between 1 and 4; it may be wise to only rely on 1 and 4 (initialized and complete).
For a complete listing of the bugs, see
http://jpspan.sourceforge.net/wiki/doku.php?id=javascript:xmlhttprequest:behaviourAnd readystate 3, from the same page:
Mozilla re-invokes readyState 3 for every 4096 bytes of data received (or end of file – whichever comes first). Meanwhile IE invokes readyState 3 once then waits until the full response has been received before switching to readyState 4
Special Effects
Please see VisualEffects.
Drag and drop
Could something like this be added?
dgm: Along with the edit in place on that page too?
I think the resortable, editable lists would be a big win. There are lots of times when that kind of sortability and in-place editability could be used.bram: I think the edittable slideshow (on the editinplace page) is also very usable…