mattmccray

 
« Enhancing RoR's AJAX.… | Home | Do, or do not -- neve… »

The Right Way

» Published March 25, 2005 under Rails, Software

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'),
                     :update        => 'my-target-div',
                     :update_method => 'after_begin'
) %>

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.


2 comments

03.26.05 @ 09:19 Andrew said...
“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.
03.26.05 @ 10:55 M@ said...
Ah, you’re right… That’s a typo. It should be: before_begin, after_begin, before_end, after_end, or replace.

Why are there so many different methods? That’s a good question. For any DOM element there are five places you can put new content, relative to the original element. For example, if I have a DIV with the id of myDiv, I could say that I want new content:

1. Added before, but outside of myDiv (before_begin)
2. Added at the beginning, inside of myDiv (after_begin)
3. Added at the bottom of myDiv, but still inside the div (before_end)
4. Added after and outside of myDiv (after_end)
5. Replacing the content of myDiv (replace)

The nomenclature is a bit bulky, but that’s because it’s patterned after IE’s insertAdjacentHTML. :)

It’s probably more clear to say: before, top, bottom, after, replace

No trackbacks

Trackback link:

Please enable javascript to generate a trackback url


You may use Textile, or simple html tags (B,I). Feel free to use Emoticons too. Oh, and please limit yourself to only five links per comment. Anything more and you'll probably get detained by the spam police.