Just a quick question about the element parameter to Event.observe, in
prototype 1.6.0 ...

I'm used to DOM scripting, and I'm wondering if a behavior of
prototype is intentional or not.
When the documentation says
  "The DOM element you want to observe; as always in Prototype, this
can be either an actual DOM reference, or the ID string for the
element."
and I try to give it a DOM element node, I get the error "element has
no properties"...
The localized function where this happens is:

function getEventID(element) {
 if (element._eventID) return element._eventID;    //<= The error
happens here...
 arguments.callee.id = arguments.callee.id || 1;
 return element._eventID = ++arguments.callee.id;
}

Now, If I add an id on the node, and I give this id to observe(),
everything works just fine, but can we use the DOM element, or just
the HTML element after having fetched it's reference through $()?

For more understanding, this is where I had this issue.
I scan my DOM tree at load time, to find every js:ajax attributes on
form elements.
When I find one, I want to add the handler to replace the standard GET/
POST mechanism with an Ajax.Request() call, binded to the form.
So, I have in my HTML:
<form action="/actions/login" method="post" js:ajax="on">
...
</form>

And my JS object prototype is as follow:
ajaxFrm.prototype={
  init:function(e){
    var aryFrms=document.getElementsByTagName('form');
    var elm;
    var i=0;
    var att=null;
    for(i=0;i<aryFrms.length;i++){
      elm=aryFrms[i];
      att=elm.getAttribute('js:ajax');
      if(att!==null && att.toLowerCase()!=='off'){
        //Apply an ajax request rather than the original form
        elm.pars="ajax=true&amp;hash="+this.hash;

        //We process the return of the request
        elm.process=function(req){
          alert(req.responseText);
        }

        elm.doCall=function(e){
          var elm=e.elment();
          elm.req=new new Ajax.Request( elm.action, {
                                        method: 'post',
                                        parameters: elm.pars,
                                        onComplete: elm.process
                                        }
                );
        }

        //We add the onSubmit event handler
        Event.observe(elm,'submit', elm.doCall);

        //we get every input except the submit
        var aryInp=elm.getElementsByTagName('input');
        for(var j=0;j<aryInp.length;j++){
          if(
              (aryInp[j].type.toLowerCase()!='submit')
              &&
              (aryInp[j].type.toLowerCase()!='file')
            ){
            if(pars.length>0){
              pars+="&amp;";
            }
            elm.pars+=aryInp[j].name+"="+aryInp[j].value;
          }
        }
      }
    }
  }
}

So, I try to add an event observer on the DOM node elm, which is a
shortcut to the element fetched through getElementsByTagName() ad I
get this error.
As I said, if I add an id on the forms, and address the element via
this id, it works, so it's no big deal to me, as I just have to
remember to put an id on every elements I want to observe, but this
looks a bit overkill to me.
Or am I using it the wrong way.

Thanks.
Thierry.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to