Friday, March 25, 2005

The Right Way

rails_logo_remix_small.gif OK, I got the head's up that my AJAX.Updater extension wasn't happy in IE... Not too surprising, really. The XMLHttpRequest is an ActiveXObject in IE, so it doesn't like you adding spurious properties to it in JS -- which I had to do to make my enhancements just 'drop-in'.

As I was thinking about this, something I wrote in that last post annoyed me -- I talked about doing it the right way, and then I went and completely did something different. Man, I was at my last job too long.

Anyway, instead of doing something that I knew wasn't what needed to be done, I rolled up my sleeves and submitted a patch to RoR's dev site.

So, if that patch gets approved you'll be able to use a new parameter, :update_method, to indicate how you want to update the target DIV:

<%= form_remote_tag( :url => url_for( :action=>'ajax_method'), ) %>

If the update method is replace, it will replace the target DIV's content using innerHTML. Otherwise, it will add the new content positionally (without overwriting any content in the target DIV) using either insertAdjacentHTML or a DOM equivalent -- a new JS object, Insert, handles this.

Rails will pass the :update_method ( before_begin, after_begin, before_end, after_end, or replace ) to the AJAX.Updater as an option (method param). Which is the 'right way' to do it. This way we don't need to have that hacky, :loading=>'request.prepend=true;', crap.

Also, the Insert dom helper will now prefer a native implementation of insertAdjacentHTML. If it can't find one, it will revert to the DOM implementation.

1 comment:

  1. "before_begin, after_begin, before_begin, before_end, or replace"
    I'm confused, why is "before_begin" in there twice? Maybe you mean "before_end" and "after_end"? What does putting something "after" the "begin" mean exactly? I only see three options: at the beginning, at the end, or replace everything.

    ReplyDelete